gpt4 book ai didi

c++ - 带赋值运算符的右值引用

转载 作者:太空狗 更新时间:2023-10-29 19:49:28 25 4
gpt4 key购买 nike

在这篇文章中http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1877 :

T& T::operator=(T const& x) // x is a reference to the source
{
T tmp(x); // copy construction of tmp does the hard work
swap(*this, tmp); // trade our resources for tmp's
return *this; // our (old) resources get destroyed with tmp
}

但鉴于复制省略,该公式显然效率低下!现在“很明显”编写 copy-and-swap 作业的正确方法是:

T& operator=(T x)    // x is a copy of the source; hard work already done
{
swap(*this, x); // trade our resources for x's
return *this; // our (old) resources get destroyed with x
}

据说出于复制省略的考虑,我们应该用值参数而不是常量引用来编写赋值运算符。

有了右值引用特性,像下面这样写赋值运算符会更好吗? :

T& operator=(T&& x)  
{
swap(*this, x);
return *this;
}

终于没有区别了?

最佳答案

某些类型使用交换/分配习语比其他类型做得更好。这是一个做得不好的:

#include <cstddef>
#include <new>
#include <utility>

template <class T>
class MyVector
{
T* begin_;
T* end_;
T* capacity_;

public:
MyVector()
: begin_(nullptr),
end_(nullptr),
capacity_(nullptr)
{}

~MyVector()
{
clear();
::operator delete(begin_);
}

MyVector(std::size_t N, const T& t)
: MyVector()
{
if (N > 0)
{
begin_ = end_ = static_cast<T*>(::operator new(N*sizeof(T)));
capacity_ = begin_ + N;
for (; N > 0; --N, ++end_)
::new(end_) T(t);
}
}

MyVector(const MyVector& v)
: MyVector()
{
std::size_t N = v.size();
if (N > 0)
{
begin_ = end_ = static_cast<T*>(::operator new(N*sizeof(T)));
capacity_ = begin_ + N;
for (std::size_t i = 0; i < N; ++i, ++end_)
::new(end_) T(v[i]);
}
}

MyVector(MyVector&& v)
: begin_(v.begin_),
end_(v.end_),
capacity_(v.capacity_)
{
v.begin_ = nullptr;
v.end_ = nullptr;
v.capacity_ = nullptr;
}

#ifndef USE_SWAP_ASSIGNMENT

MyVector& operator=(const MyVector& v)
{
if (this != &v)
{
std::size_t N = v.size();
if (capacity() < N)
{
clear();
::operator delete(begin_);
begin_ = end_ = static_cast<T*>(::operator new(N*sizeof(T)));
capacity_ = begin_ + N;
}
std::size_t i = 0;
T* p = begin_;
for (; p < end_ && i < N; ++p, ++i)
(*this)[i] = v[i];
if (i < N)
{
for (; i < N; ++i, ++end_)
::new(end_) T(v[i]);
}
else
{
while (end_ > p)
{
--end_;
end_->~T();
}
}
}
return *this;
}

MyVector& operator=(MyVector&& v)
{
clear();
swap(v);
return *this;
}

#else

MyVector& operator=(MyVector v)
{
swap(v);
return *this;
}

#endif

void clear()
{
while (end_ > begin_)
{
--end_;
end_->~T();
}
}

std::size_t size() const
{return static_cast<std::size_t>(end_ - begin_);}
std::size_t capacity() const
{return static_cast<std::size_t>(capacity_ - begin_);}
const T& operator[](std::size_t i) const
{return begin_[i];}
T& operator[](std::size_t i)
{return begin_[i];}
void swap(MyVector& v)
{
std::swap(begin_, v.begin_);
std::swap(end_, v.end_);
std::swap(capacity_, v.capacity_);
}
};

template <class T>
inline
void
swap(MyVector<T>& x, MyVector<T>& y)
{
x.swap(y);
}

#include <iostream>
#include <string>
#include <chrono>

int main()
{
MyVector<std::string> v1(1000, "1234567890123456789012345678901234567890");
MyVector<std::string> v2(1000, "1234567890123456789012345678901234567890123456789");
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::duration<double, std::micro> US;
auto t0 = Clock::now();
v2 = v1;
auto t1 = Clock::now();
std::cout << US(t1-t0).count() << " microseconds\n";

}

这是我机器的结果:

$ clang++ -std=c++0x -stdlib=libc++ -O3  test.cpp
$ a.out
23.763 microseconds
$ a.out
23.322 microseconds
$ a.out
23.46 microseconds
$ clang++ -std=c++0x -stdlib=libc++ -O3 -DUSE_SWAP_ASSIGNMENT test.cpp
$ a.out
176.452 microseconds
$ a.out
219.474 microseconds
$ a.out
178.15 microseconds

我的观点:不要陷入相信 Elixir 或“做任何事的正确方法”的陷阱。复制/交换惯用语被超卖了。有时是合适的。但它并不总是合适的。仔细的设计和仔细的测试是无可替代的。

关于c++ - 带赋值运算符的右值引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8848363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com