C4996 std::basic_string错误解决方案

简介: C4996 std::basic_string错误解决方案

使用basic_string类的成员函数copy()时会报C4996错误


str1.copy(str, 1, 0);


//报错:
/*
错误  C4996
'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::copy': Call to 'std::basic_string::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'  
*/


查找MSDN得知,该成员函数因为可能存在下标越界风险已经被弃用,并提供了basic_string::_Copy_s 代替 basic_string::copy


代替后:


str1._Copy_s(str, 1, 0);
//通过编译


也可以通过预处理禁用该警告的方式消除警告:


#pragma warning(disable:4996)
str1.copy(str, 1, 0);
//无警告,通过编译。
相关文章
|
1月前
|
存储 安全 API
C++ 17 新特性 C++ String View:了解C++ 17 std::string_view的使用场景
C++ 17 新特性 C++ String View:了解C++ 17 std::string_view的使用场景
109 2
|
9天前
|
存储 程序员 C++
stoll函数和std::to_string函数
stoll函数和std::to_string函数
8 0
|
1月前
|
C++
C++ std::string类的使用
C++ std::string类的使用
24 0
|
8月前
|
C++
std::string 不能跨dll的一种解决方法
std::string 不能跨dll的一种解决方法
|
8月前
|
C++
MFC exe使用C++ dll中的std::string 崩溃
MFC exe使用C++ dll中的std::string 崩溃
VC7(VS2002)调试时 std::string 超过15字符乱码问题
VC7(VS2002)调试时 std::string 超过15字符乱码问题
|
9月前
C++17新特性之std::string_view
std::string_view系C++17标准发布后新增的内容,类成员变量包含两个部分:字符串指针和字符串长度,相比std::string, std::string_view涵盖了std::string的所有只读接口。如果生成的std::string无需进行修改操作,可以把std::string转换为std::string_view,std::string_view记录了对应的字符串指针和偏移位置,无需管理内存,相对std::string拥有一份字符串拷贝,如字符串查找和拷贝,效率更高。
C++17新特性之std::string_view
|
存储 程序员 C++
C++ 中的 std::string 类
C++ 在其定义中有一种将字符序列表示为 class 对象的方法。这个类叫做 std::string。String 类将字符存储为具有允许访问单字节字符的功能的字节序列。
116 0
|
C++
C++ 编程std::string类
td::string是C++标准库中的一个类,它用于表示字符串,在C++中是一个非常常用的数据类型。std::string可以保存任意长度的字符串,并且支持各种字符串操作,包括连接、查找、替换等等。
157 0
|
3天前
Failed to bind properties under ‘logging.level‘ to java.util.Map java.lang.String, java.lang.String
Failed to bind properties under ‘logging.level‘ to java.util.Map java.lang.String, java.lang.String
4 0