【发布时间】:2019-03-21 05:07:08
【问题描述】:
我花了一段时间,但我终于构建了一个最小的例子来说明我遇到的问题。
#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 ==========
【问题讨论】:
-
你为什么用VS2012?
-
@MathematicalOrchid 这是不正确的。所有最新版本的 VS 都支持 Windows XP。您只需将平台工具集设置为与 XP 兼容的工具集。当你安装 VS 时,它要么是自带的,要么是作为一个选项,你可以在安装时检查。我最近将它与 VS 2015 一起使用,并且我已经看到它与 VS 2017 一起使用。除非你的意思是它是在 XP 上运行的最新 VS。这可能是真的。
-
VS 2012 有可笑的 C++11 支持。要获得全面支持,您需要 VS 2015(我认为是更新 3)或 2017。
-
@MathematicalOrchid 如果您需要说服任何人升级:msdn.microsoft.com/en-us/library/hh567368.aspx
标签: c++ gcc visual-studio-2012