编辑:已解决见评论 --不知道如何在没有答案的情况下标记为已解决。
在观看了有关 c++0x 中完美转发/移动语义的第 9 channel 视频后,我有点相信这是编写新赋值运算符的好方法。
#include <string>
#include <vector>
#include <iostream>
struct my_type
{
my_type(std::string name_)
: name(name_)
{}
my_type(const my_type&)=default;
my_type(my_type&& other)
{
this->swap(other);
}
my_type &operator=(my_type other)
{
swap(other);
return *this;
}
void swap(my_type &other)
{
name.swap(other.name);
}
private:
std::string name;
void operator=(const my_type&)=delete;
void operator=(my_type&&)=delete;
};
int main()
{
my_type t("hello world");
my_type t1("foo bar");
t=t1;
t=std::move(t1);
}
这应该允许将 r 值和 const& s 分配给它。通过使用适当的构造函数构造一个新对象,然后用 *this 交换内容。这对我来说似乎是合理的,因为没有数据被复制超过它需要的。而且指针算法很便宜。
但是我的编译器不同意。 (g++ 4.6) 我得到了这些错误。
copyconsttest.cpp: In function ‘int main()’:
copyconsttest.cpp:40:4: error: ambiguous overload for ‘operator=’ in ‘t = t1’
copyconsttest.cpp:40:4: note: candidates are:
copyconsttest.cpp:18:11: note: my_type& my_type::operator=(my_type)
copyconsttest.cpp:30:11: note: my_type& my_type::operator=(const my_type&) <deleted>
copyconsttest.cpp:31:11: note: my_type& my_type::operator=(my_type&&) <near match>
copyconsttest.cpp:31:11: note: no known conversion for argument 1 from ‘my_type’ to ‘my_type&&’
copyconsttest.cpp:41:16: error: ambiguous overload for ‘operator=’ in ‘t = std::move [with _Tp = my_type&, typename std::remove_reference< <template-parameter-1-1> >::type = my_type]((* & t1))’
copyconsttest.cpp:41:16: note: candidates are:
copyconsttest.cpp:18:11: note: my_type& my_type::operator=(my_type)
copyconsttest.cpp:30:11: note: my_type& my_type::operator=(const my_type&) <deleted>
copyconsttest.cpp:31:11: note: my_type& my_type::operator=(my_type&&) <deleted>
我做错了吗?这是不好的做法(我认为没有办法测试你是否是 self 分配的)?编译器还没准备好?
谢谢
最佳答案
对复制/交换分配习语非常谨慎。它可能是次优的,尤其是在没有仔 segmentation 析的情况下应用时。即使您需要为赋值运算符提供强大的异常安全性,也可以通过其他方式获得该功能。
对于你的例子,我推荐:
struct my_type
{
my_type(std::string name_)
: name(std::move(name_))
{}
void swap(my_type &other)
{
name.swap(other.name);
}
private:
std::string name;
};
这将为您提供隐式复制和移动语义,这些语义转发到 std::string 的复制和移动成员。 std::string 的作者最清楚如何完成这些操作。
如果你的编译器还不支持隐式移动生成,但支持默认的特殊成员,你可以这样做:
struct my_type
{
my_type(std::string name_)
: name(std::move(name_))
{}
my_type(const mytype&) = default;
my_type& operator=(const mytype&) = default;
my_type(mytype&&) = default;
my_type& operator=(mytype&&) = default;
void swap(my_type &other)
{
name.swap(other.name);
}
private:
std::string name;
};
如果您只是想明确说明您的特殊成员,您也可以选择执行上述操作。
如果您正在处理的编译器还不支持默认的特殊成员(或隐式移动成员),那么您可以显式提供编译器在完全符合 C++11 时最终应该默认的内容:
struct my_type
{
my_type(std::string name_)
: name(std::move(name_))
{}
my_type(const mytype& other)
: name(other.name) {}
my_type& operator=(const mytype& other)
{
name = other.name;
return *this;
}
my_type(mytype&& other)
: name(std::move(other.name)) {}
my_type& operator=(mytype&& other)
{
name = std::move(other.name);
return *this;
}
void swap(my_type &other)
{
name.swap(other.name);
}
private:
std::string name;
};
如果您确实需要强大的异常安全性进行分配,请设计一次并明确说明(编辑以包含 Luc Danton 的建议):
template <class C>
typename std::enable_if
<
std::is_nothrow_move_assignable<C>::value,
C&
>::type
strong_assign(C& c, C other)
{
c = std::move(other);
return c;
}
template <class C>
typename std::enable_if
<
!std::is_nothrow_move_assignable<C>::value,
C&
>::type
strong_assign(C& c, C other)
{
using std::swap;
static_assert(std::is_nothrow_swappable_v<C>, // C++17 only
"Not safe if you move other into this function");
swap(c, other);
return c;
}
现在您的客户可以使用 strong_assign
在效率(我的 type::operator=)或强异常安全性之间进行选择。
关于C++ 统一赋值运算符移动语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7458110/