“仅可移动类型”的问题在VC ++ 2010 [英] Problem with "moveable-only types" in VC++ 2010
问题描述
我最近安装了Visual Studio 2010 Professional RC,试用它并测试在VC ++ 2010中实现的几个C ++ 0x功能。
I recently installed Visual Studio 2010 Professional RC to try it out and test the few C++0x features that are implemented in VC++ 2010.
code> std :: vector 的 std :: unique_ptr
,没有任何问题。然而,当我尝试通过传递临时到 push_back
填充它,编译器抱怨 unique_ptr
的复制构造函数是私人的。我尝试通过移动它插入一个左值,它的工作正常。
I instantiated a std::vector
of std::unique_ptr
, without any problems. However, when I try to populate it by passing temporaries to push_back
, the compiler complains that the copy constructor of unique_ptr
is private. I tried inserting an lvalue by moving it, and it works just fine.
#include <utility>
#include <vector>
int main()
{
typedef std::unique_ptr<int> int_ptr;
int_ptr pi(new int(1));
std::vector<int_ptr> vec;
vec.push_back(std::move(pi)); // OK
vec.push_back(int_ptr(new int(2))); // compiler error
}
结果是,问题不是 unique_ptr
也不是 vector :: push_back
,但是VC ++在处理右值时解决重载的方式,如下面的代码所示:
As it turns out, the problem is neither unique_ptr
nor vector::push_back
but the way VC++ resolves overloads when dealing with rvalues, as demonstrated by the following code:
struct MoveOnly
{
MoveOnly() {}
MoveOnly(MoveOnly && other) {}
private:
MoveOnly(const MoveOnly & other);
};
void acceptRValue(MoveOnly && mo) {}
int main()
{
acceptRValue(MoveOnly()); // Compiler error
}
编译器声称拷贝构造函数不可访问。如果我把它公开,程序编译(即使没有定义复制构造函数)。
The compiler complains that the copy constructor is not accessible. If I make it public, the program compiles (even though the copy constructor is not defined).
我误解了一些关于右值引用的东西,或者是一个)bug在VC ++ 2010中实现这个特性?
Did I misunderstand something about rvalue references, or is it a (possibly known) bug in VC++ 2010 implementation of this feature?
推荐答案
当它不应该(绑定值引用不调用拷贝构造函数,甚至理论上)时,它执行elided-copy-constructor-accessibility检查。因此,不应使用/ Za。
Unfortunately, /Za is buggy. It performs an elided-copy-constructor-accessibility check when it shouldn't (binding rvalue references doesn't invoke copy constructors, even theoretically). As a result, /Za should not be used.
Stephan T. Lavavej,Visual C ++库开发人员(stl@microsoft.com)
Stephan T. Lavavej, Visual C++ Libraries Developer (stl@microsoft.com)
这篇关于“仅可移动类型”的问题在VC ++ 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!