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

后端 未结 6 1799
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 04:16

I have a simple C++ with Boost like this:

#include 

int main()
{
  std::string latlonStr = \"hello,ergr()()rg(rg)\";
  boo         


        
相关标签:
6条回答
  • 2020-12-03 04:44

    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 
    
    0 讨论(0)
  • 2020-12-03 04:46

    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. ;)

    0 讨论(0)
  • 2020-12-03 04:47

    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.

    0 讨论(0)
  • 2020-12-03 04:51

    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

    0 讨论(0)
  • 2020-12-03 05:03
    • Go to the properties of your C++ project

    • Expand the "C/C++"

    • Advanced: Disable Specific Warnings: 4996

    0 讨论(0)
  • 2020-12-03 05:09

    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"

    0 讨论(0)
提交回复
热议问题