forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.10.if.constexpr.cpp
More file actions
31 lines (27 loc) · 602 Bytes
/
2.10.if.constexpr.cpp
File metadata and controls
31 lines (27 loc) · 602 Bytes
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
//
// 2.10.if.constexpr.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
//
#include <iostream>
template<typename T>
auto print_type_info(const T& t) {
if constexpr (std::is_integral<T>::value) {
return t + 1;
} else {
return t + 0.001;
}
}
// at compiling time
// int print_type_info(const int& t) {
// return t + 1;
// }
// double print_type_info(const double& t) {
// return t + 0.001;
// }
int main() {
std::cout << print_type_info(5) << std::endl;
std::cout << print_type_info(3.14) << std::endl;
}