C.60: Make copy assignment non-virtual, take the parameter by const&, and return by non-const&
C.60: 拷貝賦值運算符應該是以const&爲參數,返回非常量引用類型的非虛函數
Reason(原因)
It is simple and efficient. If you want to optimize for rvalues, provide an overload that takes a && (see F.18).
因爲這樣簡單且高效。如果你希望對右值優化,提供一個使用&&(右值引用)的重載。
Example(示例)
class Foo {
public:
Foo& operator=(const Foo& x)
{
// GOOD: no need to check for self-assignment (other than performance)
auto tmp = x;
swap(tmp); // see C.83
return *this;
}
// ...
};
Foo a;
Foo b;
Foo f();
a = b; // assign lvalue: copy
a = f(); // assign rvalue: potentially move
Note(注意)
The swap implementation technique offers the strong guarantee.
實現交換函數(參考C.83)的技術提供了(不會發生自拷貝,譯者注)強有力的保證。
Example(示例)
But what if you can get significantly better performance by not making a temporary copy? Consider a simple Vector intended for a domain where assignment of large, equal-sized Vectors is common. In this case, the copy of elements implied by the swap implementation technique could cause an order of magnitude increase in cost:
但是能不能通過少進行一次臨時的拷貝動作來得到明顯更高的性能呢?考慮用於(元素,譯者注)大小相同的巨大Vector賦值的簡單的Vector的場景。在這種情況下,通過swap技術實現的元素拷貝動作將引起成本的大幅度增加。
譯者注
前面的例子,在swap之前進行了一次拷貝構造
template<typename T>
class Vector {
public:
Vector& operator=(const Vector&);
// ...
private:
T* elem;
int sz;
};
Vector& Vector::operator=(const Vector& a)
{
if (a.sz > sz) {
// ... use the swap technique, it can't be bettered ...
return *this;
}
// ... copy sz elements from *a.elem to elem ...
if (a.sz < sz) {
// ... destroy the surplus elements in *this and adjust size ...
}
return *this;
By writing directly to the target elements, we will get the basic guarantee rather than the strong guarantee offered by the swap technique. Beware of self-assignment.
通過將數據直接寫入對象元素,我們可以得到基本的保證而不是通過swap技術提供的強保證。爲了防止自己給自己賦值。
Alternatives(可選項)
If you think you need a virtual assignment operator, and understand why that's deeply problematic, don't call it operator=. Make it a named function like virtual void assign(const Foo&). See copy constructor vs. clone().
如果你認爲你需要一個虛賦值操作運算符,而且理解它會產生很深刻的問題,別把設計成賦值運算符。將它定義爲具名函數,例如virtual void assign(const Foo&)。參考【拷貝構造函數vs克隆】。
拷貝構造vs克隆的鏈接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-copy-virtual
Enforcement(實施建議)
-
(Simple) An assignment operator should not be virtual. Here be dragons!
(簡單)賦值運算符不應該是虛函數。那樣做很危險。
-
(Simple) An assignment operator should return T& to enable chaining, not alternatives like const T& which interfere with composability and putting objects in containers.
(簡單)賦值運算符應該返回T&,這樣才能實現連續賦值。不要改成類似const T&的類型,這樣會影響組裝性並妨礙將對象放進容器中。
-
(Moderate) An assignment operator should (implicitly or explicitly) invoke all base and member assignment operators. Look at the destructor to determine if the type has pointer semantics or value semantics.
(中等)賦值運算符應該(隱式或顯式)調用所有的基類和成員的賦值運算符。觀察析構函數以決定這個類型式指針語義還是值語義。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c60-make-copy-assignment-non-virtual-take-the-parameter-by-const-and-return-by-non-const
覺得本文有幫助?歡迎點贊並分享給更多的人。
閱讀更多更新文章,請關注微信公衆號【面向對象思考】