Gistrec icon

Get value reference declaration

Gistrec | PRO | 08/07/19 09:52:35 AM UTC | 0 ⭐ | 718 👁️ | Never ⏰ | []
C++ |

700 B

|

None

|

0 👍

/

0 👎

template<typename T>
struct value_category {
    // Or can be an 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 for ensuring we inspect an expression,
// not an entity
#define VALUE_CATEGORY(expr) value_category<decltype((expr))>::value
 
 
int main() {
    int x;
 
    cout << VALUE_CATEGORY(x)       << endl; // prints lvalue
    cout << VALUE_CATEGORY(move(x)) << endl; // prints xvalue
    cout << VALUE_CATEGORY(42)      << endl; // prints prvalue
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎