forked from mcpp-community/d2mcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0.cpp
More file actions
55 lines (46 loc) · 1.69 KB
/
Copy path0.cpp
File metadata and controls
55 lines (46 loc) · 1.69 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
// d2mcpp: https://github.com/mcpp-community/d2mcpp
// license: Apache-2.0
// file: src/cpp14/tests/00-generic-lambdas/0.cpp
//
// Exercise/练习: cpp14 | 00 - generic lambdas | 泛型 lambda
//
// Tips/提示:
// - lambda 参数使用 auto, 编译器为 operator() 生成隐式模板
// - 同一个泛型 lambda 可以接受不同类型的参数
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/lambda
// - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp14/00-generic-lambdas.md
//
// 练习交流讨论: http://forum.d2learn.org/category/20
//
// Auto-Checker/自动检测命令:
//
// d2x checker generic-lambdas
//
import std;
import d2x;
int main() {
// 0. 简单泛型 lambda — identity
auto identity = [](D2X_YOUR_ANSWER x) {
return x;
};
d2x::check_eq(identity(42), 42, "identity(42) == 42");
d2x::check(identity(std::string("hello")) == "hello", "identity(std::string(\"hello\")) == \"hello\"");
d2x::check_eq(identity(3.14), D2X_YOUR_ANSWER, "identity(3.14) == D2X_YOUR_ANSWER");
// 1. 泛型 lambda 做比较
auto greater = [](auto a, auto b) {
return D2X_YOUR_ANSWER;
};
d2x::check(greater(5, 3), "greater(5, 3)");
d2x::check(greater(2.5, 1.2), "greater(2.5, 1.2)");
d2x::check(greater(std::string("z"), std::string("a")), "greater(std::string(\"z\"), std::string(\"a\"))");
// 2. 推导类型确认
auto get_type_size = [](auto x) {
return sizeof(D2X_YOUR_ANSWER);
};
d2x::check_eq(get_type_size(42), sizeof(int), "get_type_size(42) == sizeof(int)");
d2x::check_eq(get_type_size('c'), sizeof(char), "get_type_size('c') == sizeof(char)");
d2x::wait();
return 0;
}