<分区>
我花了一段时间,但我终于构建了一个最小的示例来说明我遇到的问题。
#include <memory>
#include <vector>
class Thing
{
};
class Box
{
public:
std::vector<std::unique_ptr<Thing>> Things;
static Box MakeBox() {Box b; return b;}
};
我的真实程序显然比这复杂得多。
GCC 4.8.3 愉快地编译了它。它还编译了真正的应用程序,完美运行。
Visual Studio 2012 坚持此代码不正确,在 vc\include\xmemory0
的第 606 行给我错误 C2248。如果我浏览了几英里的编译器输出,我会发现错误的真正源是上例中的第 11 行。 (定义 Things.
的行)VS 也拒绝编译我的真实应用程序,出现同样的错误。
那么,这段代码是否正确?如果它不正确,那么为什么 GCC 会接受它呢?以及如何使其正确?如果它是是正确的,那为什么 VS 不编译它呢?有什么方法可以无条件地强制 VS 实际编译我的程序吗?
VS 的输出:
1>------ Build started: Project: TestUniquePtr, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(606): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1> with
1> [
1> _Ty=Thing
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1> with
1> [
1> _Ty=Thing
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::allocator<std::unique_ptr<Thing>>
1> ]
1> d:\projects\c++\testuniqueptr\testuniqueptr\main.cpp(11) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========