Aja*_*dav 5 c++ constructor vector copy-constructor c++98
我可以在 C++98 中使用复制构造函数和赋值运算符模拟移动构造函数和移动赋值运算符的功能,以提高性能,只要我知道复制构造函数和复制赋值将只为代码中的临时对象调用,或者我在我的代码中插入针吗?眼睛?
我举了两个例子,一个是普通的复制构造函数和复制赋值运算符,另一个是模拟移动构造函数和移动赋值运算符,并在向量中推送 10000 个元素来调用复制构造函数。
普通复制构造函数和复制赋值运算符的示例(copy.cpp)
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class MemoryBlock
{
public:
// Simple constructor that initializes the resource.
explicit MemoryBlock(int length)
: _length(length)
, _data(new int[length])
{
}
// Destructor.
~MemoryBlock()
{
if (_data != NULL)
{
// Delete the resource.
delete[] _data;
}
}
//copy constructor.
MemoryBlock(const MemoryBlock& other): _length(other._length)
, _data(new int[other._length])
{
std::copy(other._data, other._data + _length, _data);
}
// copy assignment operator.
MemoryBlock& operator=(MemoryBlock& other)
{
//implementation of copy assignment
}
private:
int _length; // The length of the resource.
int* _data; // The resource.
};
int main()
{
// Create a vector object and add a few elements to it.
vector<MemoryBlock> v;
for(int i=0; i<10000;i++)
v.push_back(MemoryBlock(i));
// Insert a new element into the second position of the vector.
}
Run Code Online (Sandbox Code Playgroud)
带有复制构造函数和复制赋值运算符的模拟移动构造函数和移动赋值运算符功能的示例(move.cpp)
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class MemoryBlock
{
public:
// Simple constructor that initializes the resource.
explicit MemoryBlock(int length=0)
: _length(length)
, _data(new int[length])
{
}
// Destructor.
~MemoryBlock()
{
if (_data != NULL)
{
// Delete the resource.
delete[] _data;
}
}
// Move constructor.
MemoryBlock(const MemoryBlock& other)
{
// Copy the data pointer and its length from the
// source object.
_data = other._data;
_length = other._length;
// Release the data pointer from the source object so that
// the destructor does not free the memory multiple times.
(const_cast<MemoryBlock&>(other))._data = NULL;
//other._data=NULL;
}
// Move assignment operator.
MemoryBlock& operator=(const MemoryBlock& other)
{
//Implementation of move constructor
return *this;
}
private:
int _length; // The length of the resource.
int* _data; // The resource.
};
int main()
{
// Create a vector object and add a few elements to it.
vector<MemoryBlock> v;
for(int i=0; i<10000;i++)
v.push_back(MemoryBlock(i));
// Insert a new element into the second position of the vector.
}
Run Code Online (Sandbox Code Playgroud)
我观察到性能提高了一些成本:
$ g++ copy.cpp -o copy
$ time ./copy
real 0m0.155s
user 0m0.069s
sys 0m0.085s
$ g++ move.cpp -o move
$ time ./move
real 0m0.023s
user 0m0.013s
sys 0m0.009s
Run Code Online (Sandbox Code Playgroud)
我们可以观察到性能会随着一些成本的增加而提高。
您将无法让该语言以与 C++11 及更高版本相同的方式理解 R 值,但您仍然可以move
通过创建自定义“R 值”类型来模拟所有权转移来近似语义行为。
“移动语义”实际上只是以惯用的形式破坏性地编辑/窃取对象引用中的内容。这与从不可变视图复制到对象相反。C++11 及更高版本中在语言级别引入的惯用方法作为重载集呈现给我们,使用左值表示副本 ( const T&
),使用(可变的)右值表示移动 ( T&&
)。
尽管该语言通过使用右值引用处理生命周期的方式提供了更深层次的钩子,但我们绝对可以通过创建类似类型来模拟C++98 中的移动语义rvalue
,但它会有一些限制。我们所需要的只是一种创建重载集的方法,该重载集可以消除复制概念与移动概念的歧义。
重载集对于 C++ 来说并不是什么新鲜事,这可以通过瘦包装类型来完成,该包装类型允许使用基于标记的调度来消除重载的歧义。
例如:
// A type that pretends to be an r-value reference
template <typename T>
class rvalue {
public:
explicit rvalue(T& ref)
: _ref(&ref)
{
}
T& get() const {
return *_ref;
}
operator T&() const {
return *_ref;
}
private:
T* _ref;
};
// returns something that pretends to be an R-value reference
template <typename T>
rvalue<T> move(T& v)
{
return rvalue<T>(v);
}
Run Code Online (Sandbox Code Playgroud)
我们无法通过运算符访问成员来表现得与引用完全相同.
,因为 C++ 中不存在该功能——因此必须get()
获取引用。但我们可以发出一种在代码库中成为惯用的方法来破坏性地改变类型。
rvalue
根据您的需求,类型也可以更具创意——为了简洁起见,我只是保持简单。operator->
添加至少有一种直接访问成员的方法可能是值得的。
我遗漏了T&&
->const T&&
转换、T&&
转换U&&
(其中U
是 的基数T
)和T&&
引用折叠为T&
。这些东西可以通过使用隐式转换运算符/构造函数进行修改来引入rvalue
(但可能需要一些 light-SFINAE)。然而,我发现除了泛型编程之外很少需要这样做。对于纯粹/基本的“移动语义”,这实际上就足够了。
集成此“右值”类型就像添加重载以了解类型“移自”的rvalue<T>
位置一样简单。T
对于上面的示例,它只需要添加构造函数/移动赋值运算符:
// Move constructor.
MemoryBlock(rvalue<MemoryBlock> other)
: _length(other.get()._length),
_data(other.get()._data)
{
other.get()._data = NULL;
}
MoveBlock& operator=(rvalue<MemoryBlock> other)
{
// same idea
}
Run Code Online (Sandbox Code Playgroud)
这允许您保持复制构造函数的惯用性,并模拟“移动”构造函数。
现在的用途可以变成:
MemoryBlock mb(42);
MemoryBlock other = move(mb); // 'move' constructor -- no copy is performed
Run Code Online (Sandbox Code Playgroud)
这是编译器资源管理器上的一个工作示例,它比较了复制与移动程序集。
rvalue
没有可转化的 PR 值这种方法的一个显着限制是,您无法执行 C++11 或更高版本中发生的 PR 值到 R 值的转换,例如:
MemoryBlock makeMemoryBlock(); // Produces a 'PR-value'
...
// Would be a move in C++11 (if not elided), but would be a copy here
MemoryBlock other = makeMemoryBlock();
Run Code Online (Sandbox Code Playgroud)
据我所知,如果没有语言支持,这是无法复制的。
与 C++11 不同,不会有自动生成的移动构造函数或赋值运算符——因此,对于要添加“移动”支持的类型,这需要手动操作。
值得指出的是,因为复制构造函数和赋值运算符在某些情况下是免费的,而移动则需要手动操作。
rvalue
不是 L 值参考在 C++11 中,命名的 R 值引用是左值引用。这就是为什么您会看到如下代码:
void accept(T&& x)
{
pass_to_something_else(std::move(x));
}
Run Code Online (Sandbox Code Playgroud)
如果没有编译器支持,则无法对这种命名的右值到左值转换进行建模。这意味着rvalue
引用的行为始终类似于 R 值引用。例如:
void accept(rvalue<T> x)
{
pass_to_something_else(x); // still behaves like a 'move'
}
Run Code Online (Sandbox Code Playgroud)
简而言之,您将无法为 PR 值等内容提供完整的语言支持。但您至少可以实现一种方法,允许通过“尽力而为”的尝试有效地将内容从一种类型移动到另一种类型。如果这在代码库中得到一致采用,它可以变得与 C++11 及更高版本中正确的移动语义一样惯用。
在我看来,尽管有上面列出的限制,这种“尽力而为”是值得的,因为您仍然可以以惯用的方式更有效地转让所有权。
注意:我不建议重载两者T&
并const T&
尝试“移动语义”。这里的一个大问题是,它可能会无意中通过简单的代码变得具有破坏性,例如:
SomeType x; // not-const!
SomeType y = x; // x was moved?
Run Code Online (Sandbox Code Playgroud)
这可能会导致代码中出现错误行为,并且不易察觉。使用包装器方法至少使这种破坏更加明确
归档时间: |
|
查看次数: |
1127 次 |
最近记录: |