Visual C ++ 2010:unordered_map和move constructor [英] Visual C++ 2010 : unordered_map and move constructor

查看:250
本文介绍了Visual C ++ 2010:unordered_map和move constructor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Foo 的结构,其中包含 unique_ptr

  struct Foo {
std :: unique_ptr< Bar>指针;
};

现在我试图存储 Foo unordered_map

  std :: unordered_map< int,Foo> ; myMap; 

技术上这应该是可能的,因为地图不需要复制构造函数,只有一个移动构造函数。 / p>

但是,我无法在我的地图中插入元素:

  myMap.insert(std :: make_pair(3,Foo())); 

此行将在Visual C ++ 2010中生成以下错误(由我粗略翻译,不是英文):

 错误C2248:'std :: unique_ptr< _Ty> :: unique_ptr':无法访问私有成员在'std :: unique_ptr< _Ty>中声明
with
[
_Ty = Foo
]
c:\Softwares\Visual Studio 10.0\VC\\ \\include\ mememory(2347):请参阅声明'std :: unique_ptr< _Ty> :: unique_ptr'
with
[
_Ty = Foo
]
这个诊断发生在编译器生成的函数'Foo :: Foo(const Foo&)'

由于未知的原因,编译器尝试为 Foo 而不是move构造函数生成一个复制构造函数,并失败。



我尝试用 std :: pair< int,something> 替换 std :: make_pair 找到任何 something






编辑:这个工作

  struct Foo {
Foo(){}
Foo ;&其他):指针(std :: move(other.pointer)){}
std :: unique_ptr< Bar>指针;
};

但我的真实结构包含了很多的成员,

MSVC10(在Visual Studio 2010中)不实现隐式移动构造函数(并不奇怪,因为隐式移动构造函数进入标准很晚了 - 有很多讨论)。它们不会在MSVC11中(在尚未发布的Visual Studio 2012中)。



我建议使用 = default ,但目前还不支持。


I have a structure named Foo which contains a unique_ptr

struct Foo {
    std::unique_ptr<Bar> pointer;
};

Now I'm trying to store instances of Foo in an unordered_map

std::unordered_map<int,Foo> myMap;

Technically this should be possible, since maps do not require a copy constructor, only a move constructor.

However, I can't insert an element in my map:

myMap.insert(std::make_pair(3, Foo()));

This line will generate the following error in Visual C++ 2010 (roughly translated by me, since my compiler is not in english):

error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : unable to access private member declared in 'std::unique_ptr<_Ty>'  
with
[
             _Ty=Foo
]
c:\Softwares\Visual Studio 10.0\VC\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
with
[
             _Ty=Foo
]
This diagnostic happened in the compiler-generated function 'Foo::Foo(const Foo&)'

So for an unknown reason, the compiler tries to generate a copy constructor for Foo instead of a move constructor, and fails.

I tried to replace std::make_pair with std::pair<int,something>, but can't find any something which works.


EDIT : this works

struct Foo {
    Foo() {}
    Foo(Foo&& other) : pointer(std::move(other.pointer)) {}
    std::unique_ptr<Bar> pointer;
};

But my real structure contains a lot of members, and I don't want to write all of them in the move constructor.

解决方案

MSVC10 (in Visual Studio 2010) doesn't implement implicit move constructors yet (not surprising since implicit move constructors got into the standard quite late - there was a lot of discussion about it). They won't be in MSVC11 (in the not-yet-released Visual Studio 2012) either.

I would suggest using =default, but that's not supported yet either.

这篇关于Visual C ++ 2010:unordered_map和move constructor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆