Created
February 22, 2016 21:57
-
-
Save meshell/f6c8236ce4a35825ddf0 to your computer and use it in GitHub Desktop.
constexpr example from "Effective Modern C++" by Scott Meyers, Item 15
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 <array> | |
#include <iostream> | |
#if __cplusplus > 201103L | |
constexpr int pow(int base, int exp) noexcept | |
{ | |
auto result = 1; | |
for (int i = 0; i < exp; ++ i) { | |
result *= base; | |
} | |
return result; | |
} | |
#else | |
constexpr int pow(int base, int exp) noexcept | |
{ | |
return (exp == 0 ? 1 : base * pow(base, exp -1)); | |
} | |
#endif | |
int main() | |
{ | |
// We need to store all possible combinations of n values with 3 states | |
constexpr auto n = 5; // n is 5 | |
std::array<int, pow(3, n)> results; | |
std::cout << results.size() << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment