Created
May 22, 2015 17:55
-
-
Save willkill07/9606c4d0d9f017998a4f 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
#include <iostream> | |
#include <array> | |
#include <algorithm> | |
#include <numeric> | |
typedef unsigned long long ULL; | |
// Delcare the litparser | |
template<ULL Sum, char... Chars> struct litparser; | |
// Specialize on the case where there's at least one character left: | |
template<ULL Sum, char Head, char... Rest> | |
struct litparser<Sum, Head, Rest...> { | |
// parse a digit. recurse with new sum and ramaining digits | |
static const ULL value = litparser< | |
(Head <'0' || Head >'9') ? throw std::exception() : | |
Sum*10 + Head-'0' , Rest...>::value; | |
}; | |
// When 'Rest' finally is empty, we reach this terminating case | |
template<ULL Sum> struct litparser<Sum> { | |
static const ULL value = Sum; | |
}; | |
template<char... Chars> | |
auto operator"" _array(){ | |
return std::array<int, litparser<0,Chars...>::value>{}; | |
} | |
int main() { | |
auto a = 128_array; | |
std::iota (a.begin(), a.end(), 1); | |
for (const auto & v : a) { | |
std::cout << v << ' '; | |
} | |
std::cout << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment