【发布时间】:2012-10-21 21:59:30
【问题描述】:
我使用 VS11 并使用以下内容:
class ContextWrapper
{
public:
ContextWrapper()
{
} //it should be defaulted I *guess* in order to have automatic move constructor ?
// no support in VS11 for that now
Context* GetContext()
{
return this->context.get();
}
void SetContext(std::unique_ptr<Context> context)
{
this->context = std::move(context);
}
//ContextWrapper(ContextWrapper&& other): context(std::move(other.context))
//{
//} // I would like this to be generated by the compiler
private:
ContextWrapper(const ContextWrapper&);
ContextWrapper& operator= (const ContextWrapper&);
std::unique_ptr<Context> context;
};
我希望此类生成移动构造函数/赋值。我没有一个微不足道的构造函数的事实是我没有得到移动的原因吗?还是有其他因素影响?
【问题讨论】:
-
未生成移动构造函数,因为您声明了复制构造函数。移除私有拷贝构造函数和拷贝赋值。
-
添加一个不可复制的成员(比如
unique_ptr)已经阻止了复制特殊成员的生成,所以无论如何都不需要手动阻止它们。 -
问题是,如果我不将复制/赋值声明为私有,我会收到关于私有 unique_ptr 复制构造函数的错误。如果我不声明自己,移动构造函数编译器会尝试自动生成副本并失败
-
如果 VS11 支持它,我不知道,但你总是可以告诉编译器使用
T(T&&) = default;为你生成移动 c'tor。 -
如果您
= deleted 复制成员,您将生成移动成员。但是,VC++似乎根本没有实现move成员的生成。
标签: c++ visual-c++ c++11 visual-studio-2012 move-semantics