support bazel complie this project and format code.
This commit is contained in:
39
learn_class/modern_cpp_30/obj/BUILD
Normal file
39
learn_class/modern_cpp_30/obj/BUILD
Normal file
@@ -0,0 +1,39 @@
|
||||
# please run `bazel run //learn_class/modern_cpp_30/obj:obj1`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/obj:obj4`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/obj:obj5`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/obj:obj3`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/obj:all`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/obj:obj2`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "obj1",
|
||||
srcs = ["obj1.cpp"],
|
||||
copts = ["-std=c++11"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "obj4",
|
||||
srcs = ["obj4.cpp"],
|
||||
copts = ["-std=c++11"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "obj5",
|
||||
srcs = ["obj5.cpp"],
|
||||
copts = ["-std=c++11"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "obj3",
|
||||
srcs = ["obj3.cpp"],
|
||||
copts = ["-std=c++11"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "all",
|
||||
srcs = ["all.cpp"],
|
||||
copts = ["-std=c++11"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "obj2",
|
||||
srcs = ["obj2.cpp"],
|
||||
copts = ["-std=c++11"],
|
||||
)
|
93
learn_class/modern_cpp_30/obj/all.cpp
Normal file
93
learn_class/modern_cpp_30/obj/all.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
//
|
||||
// Created by light on 19-12-23.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct Test {
|
||||
Test() {
|
||||
// std::cout << "construct a Test object" << std::endl;
|
||||
}
|
||||
|
||||
Test(const Test &) {
|
||||
std::cout << "copy construct a Test object" << std::endl;
|
||||
}
|
||||
|
||||
Test &operator=(const Test &t) {
|
||||
std::cout << "copy assignment a Test object" << std::endl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Test(Test &&t) { std::cout << "move construct a Test object" << std::endl; }
|
||||
|
||||
Test &operator=(Test &&t) {
|
||||
std::cout << "move assignment a Test object" << std::endl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Test() {
|
||||
// std::cout << "destruct a Test object" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
Test getTest() {
|
||||
// anonymous object
|
||||
return Test();
|
||||
}
|
||||
|
||||
Test getTestWithName() {
|
||||
// named return value
|
||||
Test temp;
|
||||
return temp;
|
||||
}
|
||||
/**
|
||||
* 1.copy
|
||||
* construct本身在RVO和NRVO两种情况下被优化了,如果再加上move反而画蛇添足。
|
||||
* 2.加入了move assignment后,默认是调用move assignment而不是copy
|
||||
* assignment,可以将move assignment注释后测试。
|
||||
* 3.对于RVO和NRVO来说,construction的情况编译器优化得比较好了,加入move语义主要是对于assignment有比较大影响
|
||||
*/
|
||||
int main() {
|
||||
std::cout << "==== common case ====" << std::endl;
|
||||
Test o1;
|
||||
std::cout << "---- Test copy construct: " << std::endl;
|
||||
Test o2(o1); // two ways for copy construct
|
||||
Test o3 = o1;
|
||||
std::cout << "---- Test move construct: " << std::endl;
|
||||
Test o4(std::move(o3));
|
||||
std::cout << "---- Test assignment: " << std::endl;
|
||||
o2 = o1;
|
||||
std::cout << "---- Test move assignment: " << std::endl;
|
||||
Test o5;
|
||||
o5 = std::move(o4);
|
||||
|
||||
std::cout << "\n==== test for rvo ===" << std::endl;
|
||||
std::cout << "---- Test rvo for copy construct: " << std::endl;
|
||||
Test obj11(getTest());
|
||||
Test obj1 = getTest();
|
||||
std::cout << "---- Test rvo for move construct: " << std::endl;
|
||||
Test obj12(std::move(getTest()));
|
||||
std::cout << "---- Test rvo for assignment: " << std::endl;
|
||||
Test obj2;
|
||||
obj2 = getTest();
|
||||
std::cout << "---- Test rvo move assignment: " << std::endl;
|
||||
Test obj5;
|
||||
obj5 = std::move(getTest());
|
||||
|
||||
std::cout << "\n==== test for nrvo ===" << std::endl;
|
||||
std::cout << "---- Test nrvo for copy construct: " << std::endl;
|
||||
Test obj33(getTestWithName());
|
||||
Test obj3 = getTestWithName();
|
||||
std::cout << "---- Test nrvo for move construct: " << std::endl;
|
||||
Test obj34(std::move(getTestWithName()));
|
||||
std::cout << "---- Test nrvo for assignment: " << std::endl;
|
||||
Test obj4;
|
||||
obj4 = getTestWithName();
|
||||
std::cout << "---- Test nrvo move assignment: " << std::endl;
|
||||
Test obj6;
|
||||
obj6 = std::move(getTestWithName());
|
||||
|
||||
return 0;
|
||||
}
|
43
learn_class/modern_cpp_30/obj/obj1.cpp
Normal file
43
learn_class/modern_cpp_30/obj/obj1.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// Created by light on 19-12-22.
|
||||
//
|
||||
|
||||
// rvo example
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Can copy and move
|
||||
class A {
|
||||
public:
|
||||
A() { cout << "Create A\n"; }
|
||||
|
||||
~A() { cout << "Destroy A\n"; }
|
||||
|
||||
A(const A &) { cout << "Copy A\n"; }
|
||||
|
||||
A(A &&) { cout << "Move A\n"; }
|
||||
|
||||
A &operator=(const A &a) {
|
||||
std::cout << "copy assignment" << std::endl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
A &operator=(A &&a) {
|
||||
cout << "move assignment\n";
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
// 编译器可以优化 返回值移动出去
|
||||
A getA_unnamed() { return A(); }
|
||||
|
||||
int main() {
|
||||
// cout<<"构造"<<endl;
|
||||
// auto a = getA_unnamed();
|
||||
cout << "赋值" << endl;
|
||||
A aa;
|
||||
aa = getA_unnamed();
|
||||
// aa=std::move(getA_unnamed());
|
||||
}
|
46
learn_class/modern_cpp_30/obj/obj2.cpp
Normal file
46
learn_class/modern_cpp_30/obj/obj2.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by light on 19-12-22.
|
||||
//
|
||||
|
||||
// nrvo example
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Can copy and move
|
||||
class A {
|
||||
public:
|
||||
A() { cout << "Create A\n"; }
|
||||
|
||||
~A() { cout << "Destroy A\n"; }
|
||||
|
||||
A(const A &) { cout << "Copy A\n"; }
|
||||
|
||||
A(A &&) { cout << "Move A\n"; }
|
||||
|
||||
A &operator=(const A &a) {
|
||||
std::cout << "copy assignment" << std::endl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
A &operator=(A &&a) {
|
||||
cout << "move assignment\n";
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
// 编译器可以优化 返回值移动出去
|
||||
A getA_named() {
|
||||
A a;
|
||||
return a;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// cout<<"拷贝"<<endl;
|
||||
// auto a = getA_named();
|
||||
|
||||
cout << "赋值" << endl;
|
||||
A aa;
|
||||
aa = getA_named();
|
||||
}
|
48
learn_class/modern_cpp_30/obj/obj3.cpp
Normal file
48
learn_class/modern_cpp_30/obj/obj3.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Created by light on 19-12-22.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Can copy and move
|
||||
class A {
|
||||
public:
|
||||
A() { cout << "Create A\n"; }
|
||||
|
||||
~A() { cout << "Destroy A\n"; }
|
||||
|
||||
A(const A &) { cout << "Copy A\n"; }
|
||||
|
||||
A(A &&) { cout << "Move A\n"; }
|
||||
|
||||
A &operator=(const A &a) {
|
||||
std::cout << "copy assignment" << std::endl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
A &operator=(A &&a) {
|
||||
cout << "move assignment\n";
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
// 编译器无法优化
|
||||
A getA_duang() {
|
||||
A a1;
|
||||
A a2;
|
||||
if (rand() > 42) {
|
||||
return a1;
|
||||
} else {
|
||||
return a2;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// cout<<"拷贝"<<endl;
|
||||
// auto a = getA_duang();
|
||||
|
||||
cout << "赋值" << endl;
|
||||
A aa;
|
||||
aa = getA_duang();
|
||||
}
|
31
learn_class/modern_cpp_30/obj/obj4.cpp
Normal file
31
learn_class/modern_cpp_30/obj/obj4.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by light on 19-12-22.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Can copy and move
|
||||
class A {
|
||||
public:
|
||||
A() { cout << "Create A\n"; }
|
||||
|
||||
~A() { cout << "Destroy A\n"; }
|
||||
|
||||
A(const A &) { cout << "Copy A\n"; }
|
||||
|
||||
A(A &&) = delete;
|
||||
};
|
||||
|
||||
A getA_duang() {
|
||||
A a1;
|
||||
A a2;
|
||||
if (rand() > 42) {
|
||||
return a1;
|
||||
} else {
|
||||
return a2;
|
||||
}
|
||||
}
|
||||
|
||||
int main() { auto a = getA_duang(); }
|
26
learn_class/modern_cpp_30/obj/obj5.cpp
Normal file
26
learn_class/modern_cpp_30/obj/obj5.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by light on 19-12-22.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Can copy and move
|
||||
class A {
|
||||
public:
|
||||
A() { cout << "Create A\n"; }
|
||||
|
||||
~A() { cout << "Destroy A\n"; }
|
||||
|
||||
// A(const A &) { cout << "Copy A\n"; }
|
||||
//
|
||||
// A(A &&) { cout << "Move A\n"; }
|
||||
|
||||
A(const A &&) = delete;
|
||||
A(A &&) = delete;
|
||||
};
|
||||
|
||||
A getA_unnamed() { return A(); }
|
||||
|
||||
int main() { auto a = getA_unnamed(); }
|
Reference in New Issue
Block a user