Created
September 21, 2014 14:57
-
-
Save kris7t/eee659007f367c9ca793 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 <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