Compiler warnings using boost::split [duplicate] - c++

I have a simple C++ with Boost like this:
#include <boost/algorithm/string.hpp>
int main()
{
std::string latlonStr = "hello,ergr()()rg(rg)";
boost::find_format_all(latlonStr,boost::token_finder(boost::is_any_of("(,)")),boost::const_formatter(" "));
This works fine; it replaces every occurrence of ( ) , with a " "
However, I get this warning when compiling:
I'm using MSVC 2008, Boost 1.37.0.
1>Compiling...
1>mainTest.cpp
1>c:\work\minescout-feat-000\extlib\boost\algorithm\string\detail\classification.hpp(102) : warning C4996: 'std::copy': Function call 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'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2576) : see declaration of 'std::copy'
1> c:\work\minescout-feat-000\extlib\boost\algorithm\string\classification.hpp(206) : see reference to function template instantiation 'boost::algorithm::detail::is_any_ofF<CharT>::is_any_ofF<boost::iterator_range<IteratorT>>(const RangeT &)' being compiled
1> with
1> [
1> CharT=char,
1> IteratorT=const char *,
1> RangeT=boost::iterator_range<const char *>
1> ]
1> c:\work\minescout-feat-000\minescouttest\maintest.cpp(257) : see reference to function template instantiation 'boost::algorithm::detail::is_any_ofF<CharT> boost::algorithm::is_any_of<const char[4]>(RangeT (&))' being compiled
1> with
1> [
1> CharT=char,
1> RangeT=const char [4]
1> ]
I could certainly disable the warning using
-D_SCL_SECURE_NO_WARNINGS
but I'm a bit reluctant to do that before I find out what's wrong, or more importantly if my code is incorrect.

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.

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

Go to the properties of your C++ project
Expand the "C/C++"
Advanced: Disable Specific Warnings: 4996

Related

How can I avoid this warning with boost:gil:png_write_view on x64

The following code issues a warning:
boost::gil::rgb8_image_t img(10, 10);
boost::gil::png_write_view("TenByTen.png", view(img));
when compiled with VS 2010 under 64 bit. It says:
\boost\gil\extension\io\png_io_private.hpp(341): warning C4244: 'argument' : conversion from '__int64' to 'png_uint_32', possible loss of data
\boost\gil\extension\io\png_io.hpp(202) : see reference to function template instantiation 'void boost::gil::detail::png_writer::apply<View>(const View &)' being compiled
with
[
View=boost::gil::image_view<boost::gil::rgb8_loc_t>
]
main.cpp(20) : see reference to function template instantiation 'void boost::gil::png_write_view<boost::gil::image_view<Loc>>(const char *,const View &)' being compiled
with
[
Loc=boost::gil::rgb8_loc_t,
View=boost::gil::image_view<boost::gil::rgb8_loc_t>
]
It seems quite obvious that in apply() the call to png_set_IHDR() should give a png_uint_32, but view.width() seems to be a signed __int64 (maybe a ptrdiff_t).
Does anybody know what i could do about it?
I guess that boost:gil is meant to work under 64 bits.
I use boost 1_50.
It seems that the specific issue is known from (at least) Boost v1.35 (e.g. http://objectmix.com/c/733997-how-use-graphics.html#post2586573).
Checking Boost trac database I see similar tickets for other modules but nothing for GIL.
Probably the best thing is open a new ticket.
Meanwhile, since it doesn't seem "catastrophic", you could wrap the #include(s) in this way:
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4244) // Perhaps also 4100, 4127, 4512
// GIL headers
# pragma warning(pop)
#endif
See also How to: Enable and Disable Code Analysis for Specific C/C++ Warnings
(with clang/gcc you have the -isystem switch, but I don't think Visual Studio lets you differentiate between application/header files)

std::forward compiling error VS2010

I am getting a weird error that I can't understand(since it's from a native file of VS) from file utility(it's located in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\), it is being compiled in my project with Visual C++ 10.0.
'std::forward' : cannot convert parameter 1 from '' to '&'
template<class _Other1, class _Other2>
pair(_Other1&& _Val1, _Other2&& _Val2)
: _Mybase(_STD forward<_Other1>(_Val1), _STD forward<_Other2>(_Val2))
{ // construct from moved values
}
Can anyone explain this error? It seems to me very strange, but I'm totally new to C++ and I don't understand how to fix it.
The error seems to be here:
std::map<std::string, DWORD>::value_type("SOME_STRING", 8192);
And the compilation reports this:
see reference to function template instantiation
'std::pair<_Ty1,_Ty2>::pair<const char(&)[12],>(_Other1,_Other2 &&)'
being compiled with
[
_Ty1=const std::string,
_Ty2=DWORD,
_Other1=const char (&)[12],
_Other2=
]
It seems the compiler is incapable of coping with the string literal passed in. I don't know whether it's a bug in the standard library implementation VS2010 uses, or whether it hits an edge case in the standard (and is actually supposed to fail), but either way, you can solve it by explicitly creating a std::string from the literal:
std::map<std::string, DWORD>::value_type(std::string("SOME_STRING"), 8192);

How can I test the portability of a C++ codebase developed and managed in Visual Studio 2010?

I have a very large and very old C++ project currently maintained in Visual Studio 2010. One member of our team has just tested the bumpiness of the upgrade path to VS 2012, and found we were being affected by this, through our use of Microsoft's non-standard extensions to the language.
Is there any tool we can run over our codebase that will tell us how many other non-standard extensions we're using, so we can eliminate them before they cause us any more problems?
Specify the compiler switch /Za which disables extensions.
For example, the following code:
#include <string>
void f(std::string&) {}
int main()
{
f(std::string("hello"));
}
Compiles (with warning) when /Za is not specified but fails to compile when /Za is specified with the following error:
main.cpp(7) : error C2664: 'f' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::string &'
with
[
_Elem=char,
_Traits=std::char_traits,
_Ax=std::allocator
]
A non-const reference may only be bound to an lvalue

Why is MSVC10 ignoring my #pragma warning(disable: 4251)?

So the 4251 warning is a serious base of at least microsoft's compiler. Essentially, any time you use a template class or non-dllexport class anywhere in the header definition you will get this warning. Better yet, there are numerous classes in stl (like map) where you can't even get rid of this error, period.
My problem is, is that for a specific class, the "#pragma warning(disable: 4251)" just flat out isn't working. It is working everywhere else but here.
DelWestInspectionProgram.cpp
#include "stdafx.h"
#pragma warning(disable: 4251)
...
Output Log:
E:\svn\VisionNow\VisionSuite\VI.Inspector.ImageProcessing.Common\BIImage.h(79): warning C4251: 'BIImage::_data' : class 'boost::shared_array<T>' needs to have dll-interface to be used by clients of class 'BIImage'
6> with
6> [
6> T=unsigned char
6> ]
6>E:\svn\VisionNow\VisionSuite\VI.Inspector.ImageProcessing.Operators\BIImageOperator.h(25): warning C4251: 'BIImageOperator::_savePath' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'BIImageOperator'
6> with
6> [
6> _Elem=char,
6> _Traits=std::char_traits<char>,
6> _Ax=std::allocator<char>
6> ]
Any help would be greatly appreciated.
From my own experience there are two issues: when it is a DLL import and when it is a DLL export.
Where it is the dllexport, Microsoft seems to ignore the #pragma warning but this is just the one project so easy to put into the compiler settings to ignore this warning. I found if you put it there, Microsoft does not issue the warning.
Where it is the dllimport, i.e. the file using your header. I found there Microsoft does not ignore the pragma and does not issue the warning, so putting it in both places will fix your issue.
My guess is that the 79 projects that suppress the warning fine are those ones that are not the DLL itself.
The warning is probably there for a reason, that being that if your client library is using a different version of boost, or even a different compiler setting, it might break.
As far as I know, some errors cannot be disabled. It is that way with the Linker. Therefore I would assume it is the same way with some compiler too. But in general I would put it inside your stdafx.h file, or at least before it. Putting it after doesn't guarantee anything, especially since we can't see your code.
[EDIT]
On the other hand that is a serious warning that I would fix if I were you and not just ignore it. If you search here on stack overflow you will find some great discussions about why it is a problem.

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

I have a simple C++ with Boost like this:
#include <boost/algorithm/string.hpp>
int main()
{
std::string latlonStr = "hello,ergr()()rg(rg)";
boost::find_format_all(latlonStr,boost::token_finder(boost::is_any_of("(,)")),boost::const_formatter(" "));
This works fine; it replaces every occurrence of ( ) , with a " "
However, I get this warning when compiling:
I'm using MSVC 2008, Boost 1.37.0.
1>Compiling...
1>mainTest.cpp
1>c:\work\minescout-feat-000\extlib\boost\algorithm\string\detail\classification.hpp(102) : warning C4996: 'std::copy': Function call 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'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2576) : see declaration of 'std::copy'
1> c:\work\minescout-feat-000\extlib\boost\algorithm\string\classification.hpp(206) : see reference to function template instantiation 'boost::algorithm::detail::is_any_ofF<CharT>::is_any_ofF<boost::iterator_range<IteratorT>>(const RangeT &)' being compiled
1> with
1> [
1> CharT=char,
1> IteratorT=const char *,
1> RangeT=boost::iterator_range<const char *>
1> ]
1> c:\work\minescout-feat-000\minescouttest\maintest.cpp(257) : see reference to function template instantiation 'boost::algorithm::detail::is_any_ofF<CharT> boost::algorithm::is_any_of<const char[4]>(RangeT (&))' being compiled
1> with
1> [
1> CharT=char,
1> RangeT=const char [4]
1> ]
I could certainly disable the warning using
-D_SCL_SECURE_NO_WARNINGS
but I'm a bit reluctant to do that before I find out what's wrong, or more importantly if my code is incorrect.
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.
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
Go to the properties of your C++ project
Expand the "C/C++"
Advanced: Disable Specific Warnings: 4996