Skip to content

Instantly share code, notes, and snippets.

@jamboree
Created August 22, 2014 09:30
Show Gist options
  • Save jamboree/0d868e42d3446580201c to your computer and use it in GitHub Desktop.
Save jamboree/0d868e42d3446580201c to your computer and use it in GitHub Desktop.
#include <iostream>
template<char...>
struct str {};
template<char c>
struct ch {};
template<int n>
using idx = std::integral_constant<int, n>;
template<class S, char c, char... cs>
auto accum(S s, ch<c>, str<cs...>)
{
return accum(s, s(idx<sizeof...(cs) + 1>()), str<cs..., c>());
}
template<class S, char... cs>
auto accum(S, ch<0>, str<cs...> ret)
{
return ret;
}
template<class S>
auto make_str(S s)
{
return accum(s, s(idx<0>()), str<>());
}
#define LIT(s) make_str([](auto idx) { return ch<s[idx.value]>(); })
template<char... cs>
std::ostream& operator<<(std::ostream& out, str<cs...>)
{
static constexpr char s[] = {cs...};
out.write(s, sizeof(s));
return out;
}
auto parse_int_impl(str<>, int i)
{
return i;
}
constexpr bool is_digit(char c)
{
return '0' <= c && c <= '9';
}
template<bool cond>
struct if_impl
{
template<class F1, class F2>
static auto call(F1& f1, F2&)
{
return f1();
}
};
template<>
struct if_impl<false>
{
template<class F1, class F2>
static auto call(F1&, F2& f2)
{
return f2();
}
};
struct parse_error {};
constexpr auto return_parse_error = []
{
return parse_error();
};
template<bool cond, class F1, class F2>
auto if_(F1&& f1, F2&& f2)
{
return if_impl<cond>::call(f1, f2);
}
template<bool cond, class F1>
auto if_(F1&& f1)
{
return if_impl<cond>::call(f1, return_parse_error);
}
template<char c, char... cs>
auto parse_int_impl(str<c, cs...>, int i)
{
return if_<is_digit(c)>([=]
{
return parse_int_impl(str<cs...>(), i * 10 + (c - '0'));
});
}
template<char... cs>
auto parse_int(str<cs...> s)
{
return if_<sizeof...(cs)>([=]
{
return parse_int_impl(s, 0);
});
}
int main(int argc, char** argv)
{
std::cout << (parse_int(LIT("123")) + 200) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment