-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy path3.cpp
More file actions
44 lines (36 loc) · 1.16 KB
/
Copy path3.cpp
File metadata and controls
44 lines (36 loc) · 1.16 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
// d2mcpp: https://github.com/mcpp-community/d2mcpp
// license: Apache-2.0
// file: src/cpp11/tests/00-auto-and-decltype/3.cpp
//
// Exercise/练习: cpp11 | 00 - auto and decltype | 函数返回值类型推导
//
// Tips/提示: 用后置返回 auto ... -> decltype(a - b) 让模板函数自动推导返回类型
//
// 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-3
//
import std;
import d2x;
// 3. 函数返回值类型
auto add_func(int a, double b) -> decltype(a + b) {
return a + b;
}
template<typename T1, typename T2>
D2X_YOUR_ANSWER minus_func(T1 a, T2 b) -> D2X_YOUR_ANSWER {
return a - b;
}
int main() {
d2x::check_eq(minus_func(1, 2), -1, "minus_func(1, 2) == -1");
d2x::check_eq(minus_func(2, 1), 1, "minus_func(2, 1) == 1");
d2x::check_eq(minus_func(1, 2.1), -1.1, "minus_func(1, 2.1) == -1.1");
d2x::wait();
return 0;
}