Skip to content

Instantly share code, notes, and snippets.

@nikanos
Created November 4, 2018 16:47
Show Gist options
  • Save nikanos/c6396815bffd8610de39b6d350f789e9 to your computer and use it in GitHub Desktop.
Save nikanos/c6396815bffd8610de39b6d350f789e9 to your computer and use it in GitHub Desktop.
static power calculation using c++ templates
#include <iostream>
#include <cstdint>
using namespace std;
template<int N, uint16_t P>
struct Pow
{
static const uint64_t Result = N * Pow<N, P - 1>::Result;
};
template<int N>
struct Pow<N, 0>
{
static const int Result = 1;
};
enum class PowerOfEleven :uint64_t
{
First = Pow<11, 1>::Result,
Second = Pow<11, 2>::Result,
Third = Pow<11, 3>::Result,
Fifteenth = Pow<11, 15>::Result
};
int main()
{
cout << static_cast<uint64_t>(PowerOfEleven::Second) << endl;
cout << static_cast<uint64_t>(PowerOfEleven::Fifteenth) << endl;
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment