Skip to content

Instantly share code, notes, and snippets.

@meshell
Created February 22, 2016 21:57
Show Gist options
  • Save meshell/f6c8236ce4a35825ddf0 to your computer and use it in GitHub Desktop.
Save meshell/f6c8236ce4a35825ddf0 to your computer and use it in GitHub Desktop.
constexpr example from "Effective Modern C++" by Scott Meyers, Item 15
#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