“仅可移动类型”的问题在 VC++ 2010年
我最近安装了 Visual Studio 2010 Professional RC 来尝试一下并测试 VC++ 2010 中实现的一些 C++0x 功能。
我实例化了 std::unique_ptr 的
,没有任何问题。但是,当我尝试通过将临时变量传递给 std::vector
push_back
来填充它时,编译器会抱怨 unique_ptr
的复制构造函数是私有的。我尝试通过移动来插入左值,效果很好。
#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++ 在处理右值时解决重载的方式,如以下代码所示:
struct MoveOnly
{
MoveOnly() {}
MoveOnly(MoveOnly && other) {}
private:
MoveOnly(const MoveOnly & other);
};
void acceptRValue(MoveOnly && mo) {}
int main()
{
acceptRValue(MoveOnly()); // Compiler error
}
编译器抱怨复制构造函数不可访问。如果我将其公开,程序就会编译(即使未定义复制构造函数)。
我是否误解了有关右值引用的某些内容,或者它是此功能的 VC++ 2010 实现中的一个(可能已知的)错误?
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.
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
}
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).
Did I misunderstand something about rvalue references, or is it a (possibly known) bug in VC++ 2010 implementation of this feature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,/Za 有问题。它在不应该执行的时候执行省略的复制构造函数可访问性检查(绑定右值引用不会调用复制构造函数,即使在理论上也是如此)。因此,不应使用 /Za。
Stephan T. Lavavej,Visual C++ 库开发人员([电子邮件受保护])
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++ Libraries Developer ([email protected])
首先,您需要一个关闭
)
:vec.push_back(int_ptr(new int(2))
); // 编译器错误现在,无论是第一种情况还是第二种情况,我都没有编译器错误。
我使用 Visual Studio 2010 Beta。
First of all, you need a close
)
:vec.push_back(int_ptr(new int(2))
); // compiler errorNow I have no compiler error neither the first nor the second case.
I use Visual Studio 2010 Beta.
我注意到我禁用了语言扩展 (\Za)。启用扩展后,代码将被正确编译。我仍然认为这是一个错误,因为这里提供的代码是完全标准的(据我所知)并且不依赖于任何 Microsoft 扩展。
I noticed that I had disabled language extensions (\Za). With the extensions enabled, the code gets correctly compiled. I still think this is a bug since the code presented here is perfectly standard (as far as I know) and does not rely on any Microsoft extensions.