Skip to content

Instantly share code, notes, and snippets.

@kris7t
Created September 21, 2014 14:57
Show Gist options
  • Save kris7t/eee659007f367c9ca793 to your computer and use it in GitHub Desktop.
Save kris7t/eee659007f367c9ca793 to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
#include <type_traits>
#include <string>
template <char... xs>
struct FooIntLiteral {
using result_type = int;
static int result() {
const char str[] = {xs..., '\0'};
return atoi(str);
}
};
template <char... xs>
struct FooStringLiteral {
using result_type = std::string;
static std::string result() {
const char str[] = {xs..., '\0'};
return str;
}
};
template <typename Char, Char x, Char... xs>
struct FooLiteral;
template <char x, char... xs>
struct FooLiteral<char, x, xs...>
: public std::conditional<'0' <= x && x <= '9',
FooIntLiteral<x, xs...>,
FooStringLiteral<x, xs...>>::type {
};
template <typename Char, Char... xs>
typename FooLiteral<Char, xs...>::result_type operator""_foo() {
return FooLiteral<Char, xs...>::result();
}
int main() {
std::cout << "1234"_foo * 2 << '\n' << "abcdef"_foo << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment