Skip to main content
  1. About
  2. Stack Internal
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Empirically determine value category of C++11 expression?

Each expression in C++11 has a value category. One of lvalue, xvalue or prvalue.

Is there a way to write a macro that, given any expression as an argument, will produce a string "lvalue", "xvalue" or "prvalue" as 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 could VALUE_CAT be implemented?

Answer*

Reminder: Answers generated by AI tools are not allowed due to Stack Overflow's artificial intelligence policy

Draft saved
Draft discarded

Required fields are marked with *

Cancel
6
  • 3
    I didn't realize decltype "mapped" expression value categories (lvalue, xvalue, prvalue) to reference types (lvalue reference to T, rvalue reference to T, T). Thanks.
    Andrew Tomazos
    –  Andrew Tomazos
    2013-05-19 18:39:11 +00:00
    Commented May 19, 2013 at 18:39
  • 1
    @user1131467: You have to use decltype with the expression wrapped in double-parenthesis like that. That changes how decltype deduces the expression's type.
    Nicol Bolas
    –  Nicol Bolas
    2013-05-19 18:53:48 +00:00
    Commented May 19, 2013 at 18:53
  • decltype(...) is documented in N3485 7.1.6.2/4
    Andrew Tomazos
    –  Andrew Tomazos
    2013-05-19 19:12:50 +00:00
    Commented May 19, 2013 at 19:12
  • @NicolBolas: Only if it is an id-expression. For example decltype(move(x)) will produce int&& as expected.
    Andrew Tomazos
    –  Andrew Tomazos
    2013-05-19 21:16:56 +00:00
    Commented May 19, 2013 at 21:16
  • @Nikos "named rvalue references are lvalues".
    user202729
    –  user202729
    2020-02-07 08:03:18 +00:00
    Commented Feb 7, 2020 at 8:03

lang-cpp
Morty Proxy This is a proxified and sanitized view of the page, visit original site.