What does it mean if Visual Studio 2012 throws a compilation error that should not exist for VS2012?

When I compile some class that uses syntactic serialization, I get a C2996 compilation error, see below for the post itself. If I look at this error, it seems that this error was selected only by older versions of the compiler. How can it be?

Error message:

E: \ Lib \ boost_1_54_0 \ boost / serialization / split_member.hpp (42): error C2996: "boost :: hash_combine": recursive function template definition

VS2013, VS2012, as well as VS2012 Update 4 showed this behavior.

+8
c ++ boost visual-studio compiler-errors visual-studio-2012
source share
1 answer

I had a similar problem. This seems like a mistake to me in VS2012 (and above). I was able to play this in a simple .cpp file, so I opened the connect ticket .

I believe this error is erroneously reported when all of the following conditions apply:

  • The object is deserialized using Boost Serialization somewhere in the * .cpp file.
  • The serialized object is "deep enough" (~ 5 levels - that is, A contains the vector B, B contains the vector C, etc. to E).
  • Somewhere in the same * .cpp file (not necessarily in the same function) some template function is called.

An error is no longer reported if the object is resized to be smaller, or if the call to the template function is deleted.

In addition, this error is not reproduced in VS2010. I tested Boost versions 1.49, 1.52, 1.55.

Possible workaround: in one case, the error only appeared when using boost::serialization::object_serializable (and not by default object_class_info ). Therefore, switching to object_class_info can be a workaround, except that it will break compatibility with previously serialized data.

Possible workaround # 2: hide the template function call in a separate translation unit.

The following code reproduces the error:

 /* Download Boost from boost.org and extract into "boost_1_55_0". Open VS2010 command prompt and build using the following command. It will build sucessfully. cl BugC2996.cpp /c /EHsc /MD /I"boost_1_55_0" Open VS2012 command prompt and build using the same command. It will fail with error C2996 on the line that calls UnrelatedTemplateFunc. If you add EITHER of the following definitions to the command line, it will build successfully on VS2012: /D "DONT_CALL_FUNC" /D "SER_CLASS=B" So it has something to do with the depth of the serialization, plus calling an unrelated templated function. Please only compile, don't try to link - it will naturally fail. */ #pragma warning(disable:4267) // 'var' : conversion from 'size_t' to 'type', possible loss of data (problem in Boost, unrelated to the bug in question) #include <fstream> #include <boost/archive/binary_iarchive.hpp> #include <boost/serialization/vector.hpp> #include <boost/serialization/nvp.hpp> #include <boost/serialization/map.hpp> #include <boost/serialization/version.hpp> #include <boost/serialization/export.hpp> #include <boost/serialization/access.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/list.hpp> #include <boost/serialization/shared_ptr.hpp> #include <boost/serialization/shared_ptr_132.hpp> #include <boost/serialization/split_member.hpp> #include <boost/serialization/utility.hpp> struct E { int x; }; struct D { std::vector<E> children; }; struct C { std::vector<D> children; }; struct B { std::vector<C> children; }; struct A { std::vector<B> children; }; template<class Archive> void serialize(Archive & ar, A& obj, const unsigned int /*file_version*/) { ar & BOOST_SERIALIZATION_NVP(obj.children); } template<class Archive> void serialize(Archive & ar, B& obj, const unsigned int /*file_version*/) { ar & BOOST_SERIALIZATION_NVP(obj.children); } template<class Archive> void serialize(Archive & ar, C& obj, const unsigned int /*file_version*/) { ar & BOOST_SERIALIZATION_NVP(obj.children); } template<class Archive> void serialize(Archive & ar, D& obj, const unsigned int /*file_version*/) { ar & BOOST_SERIALIZATION_NVP(obj.children); } template<class Archive> void serialize(Archive & ar, E& obj, const unsigned int /*file_version*/) { ar & BOOST_SERIALIZATION_NVP(obj.x); } template<class T> boost::shared_ptr<T> UnrelatedTemplateFunc(const std::string & argument); #ifndef SER_CLASS #define SER_CLASS A #endif // serialize A and the build will fail (if UnrelatedTemplateFunc is called below) // serialize B instead of A and the build will succeed! void Func(boost::shared_ptr<SER_CLASS> obj) { std::ifstream ifs; boost::archive::binary_iarchive ia(ifs); ia >> obj; } void OtherFunc() { // comment this line and the build will succeed, whatever struct you decide to serialize above #ifndef DONT_CALL_FUNC UnrelatedTemplateFunc<unsigned char>(""); #endif } 
+4
source share

All Articles