-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy path2.cpp
More file actions
75 lines (60 loc) · 2.11 KB
/
Copy path2.cpp
File metadata and controls
75 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// d2mcpp: https://github.com/mcpp-community/d2mcpp
// license: Apache-2.0
// file: src/cpp11/tests/11-inherited-constructors/2.cpp
//
// Exercise/练习: cpp11 | 11 - inherited constructors | 继承构造函数在泛型装饰器的应用
//
// Tips/提示: 根据编译器的输出和报错信息, 模仿NoCopy类模板, 实现一个NoMove类模板
// 让被NoMove修饰的类, 创建出来的对象不支持移动语义
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/using_declaration.html#Inheriting_constructors
// - https://mcpp-community.github.io/d2mcpp/cpp11/11-inherited-constructors.html
//
// Auto-Checker/自动检测命令:
//
// d2x checker inherited-constructors
//
import std;
import d2x;
struct Point {
double mX, mY;
Point() : mX { 0 }, mY { 0 } { }
Point(double x, double y) : mX { x }, mY { y } { }
std::string to_string() const {
return "{ " + std::to_string(mX)
+ ", " + std::to_string(mY) + " }";
}
};
template <typename T>
class NoCopy : public T {
public:
using T::T;
NoCopy(const NoCopy&) = delete;
NoCopy& operator=(const NoCopy&) = delete;
NoCopy(NoCopy&&) = default;
NoCopy& operator=(NoCopy&&) = default;
};
int main() {
Point p1(1, 1);
NoCopy<Point> p2(2, 2);
std::cout << "p1: " << p1.to_string() << std::endl;
std::cout << "p2: " << p2.to_string() << std::endl;
auto p11 = p1;
std::cout << "p11: " << p11.to_string() << std::endl;
d2x::check_eq(p1.mX, p11.mX, "p1.mX == p11.mX");
d2x::check_eq(p1.mY, p11.mY, "p1.mY == p11.mY");
decltype(p2) p22 = p2; // by std::move?
std::cout << "p22: " << p22.to_string() << std::endl;
d2x::check_eq(p2.mX, p22.mX, "p2.mX == p22.mX");
d2x::check_eq(p2.mY, p22.mY, "p2.mY == p22.mY");
NoMove<Point> p3(3, 3);
std::cout << "p3: " << p3.to_string() << std::endl;
NoMove<Point> p33(0, 0);
p33 = std::move(p3); // by copy?
std::cout << "p33: " << p33.to_string() << std::endl;
d2x::check_eq(p3.mX, p33.mX, "p3.mX == p33.mX");
d2x::check_eq(p3.mY, p33.mY, "p3.mY == p33.mY");
d2x::wait();
return 0;
}