mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-03-21 04:15:22 +08:00
support bazel complie this project and format code.
This commit is contained in:
25
learn_class/modern_cpp_30/SFINAE/sfinae paper/constexpr.cpp
Normal file
25
learn_class/modern_cpp_30/SFINAE/sfinae paper/constexpr.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by light on 20-1-6.
|
||||
//
|
||||
#include <iostream>
|
||||
|
||||
constexpr int factorial(int n) {
|
||||
return n <= 1 ? 1 : (n * factorial(n - 1));
|
||||
}
|
||||
|
||||
struct testStruct : std::true_type {
|
||||
}; // Inherit from the true type.
|
||||
|
||||
int main() {
|
||||
int i = factorial(5); // Call to a constexpr function.
|
||||
// Will be replace by a good compiler by:
|
||||
// int i = 120;
|
||||
std::cout << i << std::endl;
|
||||
|
||||
|
||||
constexpr bool testVar = testStruct(); // Generate a compile-time testStruct.
|
||||
bool test = testStruct::value; // Equivalent to: test = true;
|
||||
std::cout << test << std::endl;
|
||||
test = testVar; // true_type has a constexpr converter operator, equivalent to: test = true;
|
||||
std::cout << test << std::endl;
|
||||
}
|
||||
Reference in New Issue
Block a user