Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Created February 13, 2015 04:22
Show Gist options
  • Save yohhoy/19e7e0ebaf81b6418e69 to your computer and use it in GitHub Desktop.
Save yohhoy/19e7e0ebaf81b6418e69 to your computer and use it in GitHub Desktop.
#include <iostream>
template<typename T>
struct value_category {
static constexpr auto value = "prvalue";
};
template<typename T>
struct value_category<T&> {
static constexpr auto value = "lvalue";
};
template<typename T>
struct value_category<T&&> {
static constexpr auto value = "xvalue";
};
#define VALUE_CATEGORY(expr) value_category<decltype((expr))>::value
// UDL
struct X {};
X operator "" _x(unsigned long long) { return {}; }
int main()
{
std::cout << VALUE_CATEGORY(42) << std::endl; // integer-literal
std::cout << VALUE_CATEGORY('X') << std::endl; // character-literal
std::cout << VALUE_CATEGORY(3.14) << std::endl; // floating-literal
std::cout << VALUE_CATEGORY("ABC") << std::endl; // string-literal
std::cout << VALUE_CATEGORY(true) << std::endl; // boolean-literal
std::cout << VALUE_CATEGORY(nullptr) << std::endl; // pointer-literal
std::cout << VALUE_CATEGORY(1_x) << std::endl; // user-defined-literal
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment