mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
revision #1: 更新第二章中已维护的代码
This commit is contained in:
31
code/2/2.10.if.constexpr.cpp
Normal file
31
code/2/2.10.if.constexpr.cpp
Normal file
@@ -0,0 +1,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;
|
||||
}
|
||||
Reference in New Issue
Block a user