C++核心準則C.60: 拷貝賦值運算符應(yīng)該是以const&為參數(shù),返回非常...

C.60: 拷貝賦值運算符應(yīng)該是以const&為參數(shù),返回非常量引用類型的非虛函數(shù)
It is simple and efficient. If you want to optimize for rvalues, provide an overload that takes a && (see F.18).
因為這樣簡單且高效。如果你希望對右值優(yōu)化,提供一個使用&&(右值引用)的重載。
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 moveThe swap implementation technique offers the strong guarantee.
實現(xiàn)交換函數(shù)(參考C.83)的技術(shù)提供了(不會發(fā)生自拷貝,譯者注)強有力的保證。
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技術(shù)實現(xiàn)的元素拷貝動作將引起成本的大幅度增加。
譯者注
前面的例子,在swap之前進行了一次拷貝構(gòu)造
template
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.
通過將數(shù)據(jù)直接寫入對象元素,我們可以得到基本的保證而不是通過swap技術(shù)提供的強保證。為了防止自己給自己賦值。
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().
如果你認為你需要一個虛賦值操作運算符,而且理解它會產(chǎn)生很深刻的問題,別把設(shè)計成賦值運算符。將它定義為具名函數(shù),例如virtual void assign(const Foo&)。參考【拷貝構(gòu)造函數(shù)vs克隆】。
拷貝構(gòu)造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!
(簡單)賦值運算符不應(yīng)該是虛函數(shù)。那樣做很危險。
(Simple) An assignment operator should return T& to enable chaining, not alternatives like const T& which interfere with composability and putting objects in containers.
(簡單)賦值運算符應(yīng)該返回T&,這樣才能實現(xiàn)連續(xù)賦值。不要改成類似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.
(中等)賦值運算符應(yīng)該(隱式或顯式)調(diào)用所有的基類和成員的賦值運算符。觀察析構(gòu)函數(shù)以決定這個類型式指針語義還是值語義。
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
覺得本文有幫助?請分享給更多人。
關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
