Skip to content

Instantly share code, notes, and snippets.

@yaneury
Created May 16, 2017 12:00
Show Gist options
  • Select an option

  • Save yaneury/a75a979ecfb3be770e3c95eafd5aba55 to your computer and use it in GitHub Desktop.

Select an option

Save yaneury/a75a979ecfb3be770e3c95eafd5aba55 to your computer and use it in GitHub Desktop.
template <typename T>
constexpr T square(const T& n) {
return n * n;
}
template <typename T>
constexpr T cube(const T& n) {
return square(n) * n;
}
template <bool, typename T, T n>
struct CubeOrSquare
{
enum : T { value = square(n) };
};
template <typename T, T n>
struct CubeOrSquare<true, T, n>
{
enum : T { value = cube(n) };
};
int main() {
unsigned squared = CubeOrSquare<false, unsigned, 2>::value; // 4
unsigned cubed = CubeOrSquare<true, unsigned, 2>::value; // 8
return cubed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment