Ticket #9619: BugC2996.cpp

File BugC2996.cpp, 3.0 KB (added by Yodan Tauber <yodan.tauber@…>, 9 years ago)

Code to reproduce the problem

Line 
1/*
2Download Boost from boost.org and extract into "boost_1_55_0".
3Open VS2010 command prompt and build using the following command. It will build sucessfully.
4cl BugC2996.cpp /c /EHsc /MD /I"boost_1_55_0"
5
6Open VS2012 command prompt and build using the same command. It will fail with error C2996 on the line that calls UnrelatedTemplateFunc.
7If you add EITHER of the following definitions to the command line, it will build successfully on VS2012:
8/D "DONT_CALL_FUNC"
9/D "SER_CLASS=B"
10
11So it has something to do with the depth of the serialization, plus calling an unrelated templated function.
12
13Please only compile, don't try to link - it will naturally fail.
14*/
15
16#pragma warning(disable:4267) // 'var' : conversion from 'size_t' to 'type', possible loss of data (problem in Boost, unrelated to the bug in question)
17
18#include <fstream>
19#include <boost/archive/binary_iarchive.hpp>
20#include <boost/serialization/vector.hpp>
21#include <boost/serialization/nvp.hpp>
22#include <boost/serialization/map.hpp>
23#include <boost/serialization/version.hpp>
24#include <boost/serialization/export.hpp>
25#include <boost/serialization/access.hpp>
26#include <boost/serialization/base_object.hpp>
27#include <boost/serialization/list.hpp>
28#include <boost/serialization/shared_ptr.hpp>
29#include <boost/serialization/shared_ptr_132.hpp>
30#include <boost/serialization/split_member.hpp>
31#include <boost/serialization/utility.hpp>
32
33struct E
34{
35 int x;
36};
37
38struct D
39{
40 std::vector<E> children;
41};
42
43struct C
44{
45 std::vector<D> children;
46};
47
48struct B
49{
50 std::vector<C> children;
51};
52
53struct A
54{
55 std::vector<B> children;
56};
57
58
59template<class Archive>
60void serialize(Archive & ar, A& obj, const unsigned int /*file_version*/)
61{
62 ar & BOOST_SERIALIZATION_NVP(obj.children);
63}
64
65template<class Archive>
66void serialize(Archive & ar, B& obj, const unsigned int /*file_version*/)
67{
68 ar & BOOST_SERIALIZATION_NVP(obj.children);
69}
70
71template<class Archive>
72void serialize(Archive & ar, C& obj, const unsigned int /*file_version*/)
73{
74 ar & BOOST_SERIALIZATION_NVP(obj.children);
75}
76
77template<class Archive>
78void serialize(Archive & ar, D& obj, const unsigned int /*file_version*/)
79{
80 ar & BOOST_SERIALIZATION_NVP(obj.children);
81}
82
83template<class Archive>
84void serialize(Archive & ar, E& obj, const unsigned int /*file_version*/)
85{
86 ar & BOOST_SERIALIZATION_NVP(obj.x);
87}
88
89
90
91template<class T>
92boost::shared_ptr<T> UnrelatedTemplateFunc(const std::string & argument);
93
94
95#ifndef SER_CLASS
96#define SER_CLASS A
97#endif
98
99// serialize A and the build will fail (if UnrelatedTemplateFunc is called below)
100// serialize B instead of A and the build will succeed!
101void Func(boost::shared_ptr<SER_CLASS> obj)
102{
103 std::ifstream ifs;
104 boost::archive::binary_iarchive ia(ifs);
105 ia >> obj;
106}
107
108void OtherFunc()
109{
110 // comment this line and the build will succeed, whatever struct you decide to serialize above
111 #ifndef DONT_CALL_FUNC
112 UnrelatedTemplateFunc<unsigned char>("");
113 #endif
114}