Created
December 17, 2015 21:12
-
-
Save bwoods/111e16fef09c0ceefcd1 to your computer and use it in GitHub Desktop.
C++11 user defined literals for std::integral_constant<int64_t, N>
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
namespace literals { // inspired by http://codereview.stackexchange.com/a/51576 | |
constexpr int64_t combine(int64_t accumulator) { | |
return accumulator; | |
} | |
template <typename... Characters> | |
constexpr int64_t combine(int64_t accumulator, char character, Characters... Rest) { | |
int digit = (character >= '0' && character <= '9') ? character - '0' | |
: throw std::range_error("Only decimal literals are supported."); | |
return combine(accumulator * 10 + digit, Rest...); | |
} | |
} // end namespace | |
template<char... Characters> | |
constexpr auto operator"" _lit() { | |
return std::integral_constant<int64_t, literals::combine(0, Characters...)>{}; | |
} | |
template <int64_t N> | |
constexpr auto operator- (std::integral_constant<int64_t, N>) -> std::integral_constant<int64_t, -N> { | |
static_assert(N > 0); return {}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment