Advertisement
Gistrec

Get value reference declaration

Aug 7th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. template<typename T>
  2. struct value_category {
  3.     // Or can be an integral or enum value
  4.     static constexpr auto value = "prvalue";
  5. };
  6.  
  7. template<typename T>
  8. struct value_category<T&> {
  9.     static constexpr auto value = "lvalue";
  10. };
  11.  
  12. template<typename T>
  13. struct value_category<T&&> {
  14.     static constexpr auto value = "xvalue";
  15. };
  16.  
  17. // Double parens for ensuring we inspect an expression,
  18. // not an entity
  19. #define VALUE_CATEGORY(expr) value_category<decltype((expr))>::value
  20.  
  21.  
  22. int main() {
  23.     int x;
  24.  
  25.     cout << VALUE_CATEGORY(x)       << endl; // prints lvalue
  26.     cout << VALUE_CATEGORY(move(x)) << endl; // prints xvalue
  27.     cout << VALUE_CATEGORY(42)      << endl; // prints prvalue
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement