c++ - Empirically determine value category of C++11 expression? -
each expression in c++11 has value category. 1 of lvalue, xvalue or prvalue.
is there way write macro that, given expression argument, produce string "lvalue", "xvalue" or "prvalue" appropriate?
for example:
int main() { int x; cout << value_cat(x) << endl; // prints lvalue cout << value_cat(move(x)) << endl; // prints xvalue cout << value_cat(42) << endl; // prints prvalue }
how value_cat
implemented?
decltype
can return declared type of entity (hence name), can used query type of expression. however, in latter case resulting type 'adjusted' according value category of expression: lvalue expression results in lvalue reference type, xvalue in rvalue reference type, , prvalue in type. can use our benefit:
template<typename t> struct value_category { // or can integral or enum value 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"; }; // double parens ensuring inspect expression, // not entity #define value_category(expr) value_category<decltype((expr))>::value
Comments
Post a Comment