반응형
visual stdio 2017 ][ constexpr
■ constexpr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
// Pass by value | |
constexpr float exp(float x, int n) | |
{ | |
return n == 0 ? 1 : | |
n % 2 == 0 ? exp(x * x, n / 2) : | |
exp(x * x, (n - 1) / 2) * x; | |
}; | |
// Pass by reference | |
constexpr float exp2(const float& x, const int& n) | |
{ | |
return n == 0 ? 1 : | |
n % 2 == 0 ? exp2(x * x, n / 2) : | |
exp2(x * x, (n - 1) / 2) * x; | |
}; | |
// Compile time computation of array length | |
template<typename T, int N> | |
constexpr int length(const T(&ary)[N]) | |
{ | |
return N; | |
} | |
// Recursive constexpr function | |
constexpr int fac(int n) | |
{ | |
return n == 1 ? 1 : n*fac(n - 1); | |
} | |
// User-defined type | |
class Foo | |
{ | |
public: | |
constexpr explicit Foo(int i) : _i(i) {} | |
constexpr int GetValue() | |
{ | |
return _i; | |
} | |
private: | |
int _i; | |
}; | |
int main() | |
{ | |
//foo is const: | |
constexpr Foo foo(5); | |
// foo = Foo(6); //Error! | |
//Compile time: | |
constexpr float x = exp(5, 3); | |
constexpr float y { exp(2, 5) }; | |
constexpr int val = foo.GetValue(); | |
constexpr int f5 = fac(5); | |
const int nums[] { 1, 2, 3, 4 }; | |
const int nums2[length(nums) * 2] { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
//Run time: | |
cout << "The value of foo is " << foo.GetValue() << endl; | |
} | |
반응형