如果Visual Studio 2012抛出VS2012不存在的编译错误,这意味着什么?

ikk*_*100 8 c++ boost compiler-errors visual-studio visual-studio-2012

当我编译一些使用boost序列化的类时,我得到编译错误C2996,请参阅下面的消息本身.如果我查找此错误,似乎只有很多旧版本的编译器抛出此错误.怎么会这样?

错误消息:

E:\ Lib\boost_1_54_0\boost/serialization/split_member.hpp(42):错误C2996:'boost :: hash_combine':递归函数模板定义

VS2013,VS2012和VS2012 Update 4都显示了这种行为.

Yod*_*ber 4

我有类似的问题。在我看来,这就像 VS2012(及更高版本)中的一个错误。我已经能够在一个简单的 .cpp 文件中重现这一点,因此我打开了一个连接票证。

我相信当满足以下所有条件时会错误地报告此错误:

  1. 使用 *.cpp 文件中某处的 Boost Serialization 来反序列化对象。
  2. 序列化对象“足够深”(〜5级 - 即A包含B的向量,B包含C的向量,依此类推,直到E)。
  3. 在同一个 *.cpp 文件中的某个位置(不一定在同一个函数中),调用了一些模板化函数。

如果对象更改为更“浅”,或者删除了对模板化函数的调用,则不再报告该错误。

另外,这个错误在 VS2010 中不会重现。我测试了 Boost 版本 1.49、1.52、1.55。

可能的解决方法:在一种情况下,错误仅在我们使用时出现boost::serialization::object_serializable(而不是默认object_class_info)。因此,改回object_class_info可能是一种解决方法,除非它会破坏与先前序列化数据的向后兼容性。

可能的解决方法#2:将模板化函数调用隐藏在单独的翻译单元中。

以下代码重现了该错误:

/*
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
}
Run Code Online (Sandbox Code Playgroud)

  • 此问题在 Visual Studio 2015 Update 2 中再次出现。请参阅以下连接问题:https://connect.microsoft.com/VisualStudio/feedback/details/2584420/error-c2996-vnl-determinant-recursive-function-template-definition http ://connect.microsoft.com/VisualStudio/feedback/details/2563889/compile-error-c2996-recursive-function-template-definition-is-emissed-for-suppositly-compilable-code (2认同)