Skip to content

Instantly share code, notes, and snippets.

@willkill07
Created May 22, 2015 17:55
Show Gist options
  • Save willkill07/9606c4d0d9f017998a4f to your computer and use it in GitHub Desktop.
Save willkill07/9606c4d0d9f017998a4f to your computer and use it in GitHub Desktop.
#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