C++ Boost: what's the cause of this warning?

限于喜欢 提交于 2019-11-27 13:25:26
jalf

It is nothing to worry about. In the last few releases of MSVC, they've gone into full security-paranoia mode. std::copy issues this warning when it is used with raw pointers, because when used incorrectly, it can result in buffer overflows.

Their iterator implementation performs bounds checking to ensure this doesn't happen, at a significant performance cost.

So feel free to ignore the warning. It doesn't mean there's anything wrong with your code. It is just saying that if there is something wrong with your code, then bad things will happen. Which is an odd thing to issue warnings about. ;)

You can also disable this warning in specific headers:

#if defined(_MSC_VER) && _MSC_VER >= 1400 
#pragma warning(push) 
#pragma warning(disable:4996) 
#endif 

/* your code */ 

#if defined(_MSC_VER) && _MSC_VER >= 1400 
#pragma warning(pop) 
#endif 

If you feel safe about disabling this error:

  • Go to the properties of your C++ project
  • Expand the "C/C++"
  • Highlight "Command Line"
  • Under "Additional Options" append the following to any text that might be in that box

" -D_SCL_SECURE_NO_WARNINGS"

The warning comes from Visual Studio's non-standard "safe" library checks introduced starting with MSVC 8.0. Microsoft has identified "potentially dangerous" APIs and has injected warnings discouraging their use. While it is technically possible to call std::copy in an unsafe way, 1) receiving this warning does not mean you are doing so, and 2) using std::copy as you normally should is not dangerous.

  • Go to the properties of your C++ project

  • Expand the "C/C++"

  • Advanced: Disable Specific Warnings: 4996

Alex Azzalini

Alternatively, if you use C++11 and don't want to turn off warnings, you have the painful option of replacing

boost::is_any_of(L"(,)")

with the following lambda expression

[](wchar_t &c) { for (auto candidate : { L'(', L',', L')' }) { if (c == candidate) return true; }; return false; }

You can also possibly pack that into a macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!