jpo38
jpo38

Reputation: 21514

#pragma warning( disable: XXXX ) does not behave as expected (scope issue)

Consider this program:

#include <string>
#include <boost/random.hpp>

int main(int argc, char *argv[])
{
    return 0;
}

Compilex with VS2015, I get warning 4996 from boost\random\detail\polynomial.hpp file.

d:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2810): warning C4996: 'std::_Fill_n': 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'
  d:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2797): note: voir la déclaration de 'std::_Fill_n'
  d:\dev\vobs_ext_2015\libcpp\boost\1.60.0\boost\random\detail\polynomial.hpp(114): note: voir la référence à l'instanciation de la fonction modèle '_OutIt std::fill_n<boost::random::detail::polynomial_ops::digit_t*,size_t,boost::random::detail::polynomial_ops::digit_t>(_OutIt,_Diff,const _Ty &)' en cours de compilation
          with
          [
              _OutIt=boost::random::detail::polynomial_ops::digit_t *,
              _Diff=size_t,
              _Ty=boost::random::detail::polynomial_ops::digit_t
          ]

So I tried to disable the warning:

#include <string>

#pragma warning(push)
#pragma warning( disable: 4996 ) // disable warning coming from boost/random.hpp
#include <boost/random.hpp>
#pragma warning(pop)

int main(int argc, char *argv[])
{
    return 0;
}

Warning remains reported....then I tried:

#pragma warning(push)
#pragma warning( disable: 4996 ) // disable warning coming from boost/random.hpp
#include <string>
#include <boost/random.hpp>
#pragma warning(pop)

int main(int argc, char *argv[])
{
    return 0;
}

And now it's gone...why do I need to have #include <string> within the pragma push/pop directives?

Upvotes: 2

Views: 1237

Answers (1)

Mike Vine
Mike Vine

Reputation: 9837

The warning comes from an internal header file <xutility>. This file is included firstly by <string> and then also by <boost/random.hpp> (and its probably ignored the second time). If you want to suppress this warning you need to suppress it around the first time the header is included, but as you've found its hard to know when this is.

Its a bit of a weird warning anyway - its just saying that this call could potentially be unsafe but the compiler cant tell, not that its actually unsafe. You can simply do as the warning says and turn it off using the _SCL_SECURE_NO_WARNINGS define in your project settings (or before you include any header or in your pch if you use that)

Edit by jpo38:

This removes the warning:

#pragma warning(push)
#pragma warning( disable: 4996 ) // disable warning coming from boost/random.hpp
#include <boost/random.hpp>
#pragma warning(pop)

#include <string>

So it's true the problem comes from <xutility> being included by both <boost/random.hpp> and <string>. As <xutility> it's protected against multiple includes (it has a #pragma once), warning 4996 must be disabled while the <xutility> is included in the first place.

Upvotes: 2

Related Questions