左右值(引用)示例
- 在此处,请思考左右值相关的内容
- forward和move的作用
- namespace的使用
- 重载赋值运算符的操作
#include
using namespace std;
#define func(x) __func(x, "func(" #x ")")
#define func2(x) __func2(x, "func2(" #x ")")
void __func2(int &x, const char *str) {
cout << str << " is left value" << endl;
return ;
}
void __func2(int &&x, const char *str) {
cout << str << " is right value" << endl;
return ;
}
void __func2(double &&x, const char *str) {
cout << str << " is double value" << endl;
return ;
}
void __func(int &x, const char *str) {
cout << str << " is left value" << endl;
func2(x);
return ;
}
void __func(int &&x, const char *str) {
cout << str << " is right value" << endl;
func2(forward(x));
return ;
}
namespace haizei {
class vector {
public:
vector(int n = 10) : __size(n), data(new int[n]){
cout << "default constructor" << endl;
}
vector(const vector &v) : __size(v.size()), data(new int[__size]) {
cout << "deep copy constructor" << endl;
for (int i = 0; i < size(); i++) data[i] = v[i];
return ;
}
vector(vector &&v) : __size(v.size()), data(v.data) {
cout << "move copy constructor" << endl;
v.data = nullptr;
v.__size = 0;
}
vector operator+(const vector &v) {
vector ret(v.size() + this->size());
vector &now = *this;
for (int i = 0; i < size(); i++) {
ret[i] = now[i];
}
for (int i = size(); i < ret.size(); i++) {
ret[i] = now[i - size()];
}
return ret;
}
int &operator[](int ind) const {
return this->data[ind];
}
int size() const {return __size; }
~vector() {
if (data) delete[] data;
data = nullptr;
__size = 0;
}
private:
int __size, *data;
};
} //end of haizei
ostream &operator<<(ostream &out, const haizei::vector &v) {
for (int i = 0; i < v.size(); i++) {
out << v[i] << " ";
}
return out;
}
int main() {
haizei::vector v1, v2;
for (int i = 0; i < v1.size(); i++) v1[i] = rand() % 100;
for (int i = 0; i < v2.size(); i++) v2[i] = rand() % 100;
haizei::vector v3(v1 + v2);
cout << v1 << endl;
cout << v2 << endl;
cout << v3 << endl;
haizei::vector v4(move(v1));
cout << v1 << endl;
cout << v4 << endl;
//return 0;
int x = 1234,y = 456;
func(1234);
func(x);
func(x + y);
func(x++);
func(++x);
func(x + 123);
func(y *= 2);
func(y += 2);
func(y * 3);
return 0;
}
运行结果
不关闭编译器优化
default constructor
default constructor
default constructor
83 86 77 15 93 35 86 92 49 21
62 27 90 59 63 26 40 26 72 36
83 86 77 15 93 35 86 92 49 21 83 86 77 15 93 35 86 92 49 21
move copy constructor
83 86 77 15 93 35 86 92 49 21
func(1234) is right value
func2(forward(x)) is double value
func(x) is left value
func2(x) is left value
func(x + y) is right value
func2(forward(x)) is double value
func(x++) is right value
func2(forward(x)) is double value
func(++x) is left value
func2(x) is left value
func(x + 123) is right value
func2(forward(x)) is double value
func(y *= 2) is left value
func2(x) is left value
func(y += 2) is left value
func2(x) is left value
func(y * 3) is right value
func2(forward(x)) is double value
关掉编译器优化-fno-elide-constructors
default constructor
default constructor
default constructor
move copy constructor
move copy constructor
83 86 77 15 93 35 86 92 49 21
62 27 90 59 63 26 40 26 72 36
83 86 77 15 93 35 86 92 49 21 83 86 77 15 93 35 86 92 49 21
move copy constructor
83 86 77 15 93 35 86 92 49 21
func(1234) is right value
func2(forward(x)) is double value
func(x) is left value
func2(x) is left value
func(x + y) is right value
func2(forward(x)) is double value
func(x++) is right value
func2(forward(x)) is double value
func(++x) is left value
func2(x) is left value
func(x + 123) is right value
func2(forward(x)) is double value
func(y *= 2) is left value
func2(x) is left value
func(y += 2) is left value
func2(x) is left value
func(y * 3) is right value
func2(forward(x)) is double value