我应该如何确保对移动构造函数的调用? (移动语义和右值引用)
How should I ensure the call to the move constructor? (move semantics and rvalue reference)
我有这段代码
#include <iostream>
#include <vector>
using namespace std;
class ArrayWrapper {
private:
int *_p_vals;
int _size;
public:
//default constructor
ArrayWrapper () : _p_vals( new int[ 64 ] ) , _size( 64 ) {
cout << "default constructor called\n";
}
//constructor 2
ArrayWrapper (int n) : _p_vals( new int[ n ] ) , _size( n ) {
cout << "constructor 2 called\n";
}
//move constructor
ArrayWrapper (ArrayWrapper&& other) : _p_vals(other._p_vals), _size(other._size) {
cout << "move constructor called\n";
other._p_vals = nullptr;
other._size = 0;
}
// copy constructor
ArrayWrapper (const ArrayWrapper& other) : _p_vals( new int[ other._size ] ) , _size( other._size ) {
cout << "copy constructor called\n";
for ( int i = 0; i < _size; ++i )
_p_vals[i] = other._p_vals[i];
}
~ArrayWrapper () {
delete [] _p_vals;
}
void set_val (std::initializer_list <int> vals) {
int i = 0;
for (auto val : vals) {
_p_vals[i] = val;
i++;
}
}
void print_val () const {
for (auto i = 0; i < _size ; i++){
cout <<_p_vals[i] << ":" ;
}
cout << endl;
}
};
ArrayWrapper getArrayWrapper () {
ArrayWrapper A(5);
A.set_val({1,2,3,4,5});
return A;
}
int main () {
ArrayWrapper arr {getArrayWrapper()};
arr.print_val();
return 0;
}
我正在尝试确保正在调用移动构造函数。但是一些默认的复制构造函数正在被使用,因为输出只是 this
constructor 2 called
1:2:3:4:5:
我正在使用以下 g++ 版本
g++ (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406
我是否应该进行所有构造函数调用explicit
。我想知道这会有什么帮助。有没有办法强制程序调用我上面写的移动构造函数和复制构造函数?
感谢评论者,我发现编译器正在进行 return 值优化 (RVO),这会阻止对上面编写的构造函数的调用。我找到了一种禁用 RVO 的方法。 g++ 中的 -fno-elide-constructors
开关有效。
我有这段代码
#include <iostream>
#include <vector>
using namespace std;
class ArrayWrapper {
private:
int *_p_vals;
int _size;
public:
//default constructor
ArrayWrapper () : _p_vals( new int[ 64 ] ) , _size( 64 ) {
cout << "default constructor called\n";
}
//constructor 2
ArrayWrapper (int n) : _p_vals( new int[ n ] ) , _size( n ) {
cout << "constructor 2 called\n";
}
//move constructor
ArrayWrapper (ArrayWrapper&& other) : _p_vals(other._p_vals), _size(other._size) {
cout << "move constructor called\n";
other._p_vals = nullptr;
other._size = 0;
}
// copy constructor
ArrayWrapper (const ArrayWrapper& other) : _p_vals( new int[ other._size ] ) , _size( other._size ) {
cout << "copy constructor called\n";
for ( int i = 0; i < _size; ++i )
_p_vals[i] = other._p_vals[i];
}
~ArrayWrapper () {
delete [] _p_vals;
}
void set_val (std::initializer_list <int> vals) {
int i = 0;
for (auto val : vals) {
_p_vals[i] = val;
i++;
}
}
void print_val () const {
for (auto i = 0; i < _size ; i++){
cout <<_p_vals[i] << ":" ;
}
cout << endl;
}
};
ArrayWrapper getArrayWrapper () {
ArrayWrapper A(5);
A.set_val({1,2,3,4,5});
return A;
}
int main () {
ArrayWrapper arr {getArrayWrapper()};
arr.print_val();
return 0;
}
我正在尝试确保正在调用移动构造函数。但是一些默认的复制构造函数正在被使用,因为输出只是 this
constructor 2 called
1:2:3:4:5:
我正在使用以下 g++ 版本
g++ (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406
我是否应该进行所有构造函数调用explicit
。我想知道这会有什么帮助。有没有办法强制程序调用我上面写的移动构造函数和复制构造函数?
感谢评论者,我发现编译器正在进行 return 值优化 (RVO),这会阻止对上面编写的构造函数的调用。我找到了一种禁用 RVO 的方法。 g++ 中的 -fno-elide-constructors
开关有效。