-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy path4.cpp
More file actions
59 lines (48 loc) · 2.01 KB
/
Copy path4.cpp
File metadata and controls
59 lines (48 loc) · 2.01 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
// d2mcpp: https://github.com/mcpp-community/d2mcpp
// license: Apache-2.0
// file: src/cpp11/tests/00-auto-and-decltype/4.cpp
//
// Exercise/练习: cpp11 | 00 - auto and decltype | 类/结构体成员类型推导
//
// Tips/提示: 区分 decltype(obj) 与 decltype((obj)), 带括号是表达式会推导出引用类型
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/auto
// - https://en.cppreference.com/w/cpp/language/decltype
// - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp11/00-auto-and-decltype.md
//
// 练习交流讨论: http://forum.d2learn.org/post/357
//
// Auto-Checker/自动检测命令:
//
// d2x checker auto-and-decltype-4
//
import std;
import d2x;
// 4. 类/结构体成员类型推导
struct Object {
const int a;
double b;
Object() : a(1), b(2.0) { }
};
int main() {
const Object obj;
bool type_check = false;
// obj的类型推导 和 (obj) 的类型推导
type_check = std::is_same<decltype(obj), const Object>::value;
d2x::check(type_check, "type_check"); type_check = false; // dont change this line
type_check = std::is_same<decltype((obj)), D2X_YOUR_ANSWER>::value;
d2x::check(type_check, "type_check"); type_check = false; // dont change this line
// obj.a的类型推导 和 (obj.a) 的类型推导
type_check = std::is_same<decltype(obj.a), D2X_YOUR_ANSWER>::value;
d2x::check(type_check, "type_check"); type_check = false; // dont change this line
type_check = std::is_same<decltype((obj.a)), D2X_YOUR_ANSWER>::value;
d2x::check(type_check, "type_check"); type_check = false; // dont change this line
// obj.b的类型推导 和 (obj.b) 的类型推导
type_check = std::is_same<decltype(obj.b), D2X_YOUR_ANSWER>::value;
d2x::check(type_check, "type_check"); type_check = false; // dont change this line
type_check = std::is_same<decltype((obj.b)), D2X_YOUR_ANSWER>::value;
d2x::check(type_check, "type_check"); type_check = false; // dont change this line
d2x::wait();
return 0;
}