Created
May 16, 2017 12:00
-
-
Save yaneury/a75a979ecfb3be770e3c95eafd5aba55 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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