How to test whether expression is a temporary?

c++, c++11, static-assert

Solution

First we should clarify: What do you mean by "temporary"?

Many people mean different things when they say temporary. Technically, `int()` is not a temporary, but most people will include them into their own meaning of that term. Technically, given `std::string s;`, then `move(s)` isn't a temporary either, but you may want to treat it as one with your macro.

The first kind of "temporaries" I mentioned above are really "prvalue expressions". Those are the `std::string("foo")` or `int()` kind of things, but not the `move(s)` and also (for sure) not the `s` kind of things. The `decltype` operator yields a non-reference type for the first kind of "temporaries" I talked about above. For the second kind, `move(s)`, which are xvalues, it will yield an rvalue reference. And for the "non-temporaries", i.e the `s` cases, it will yield an lvalue reference.

So to summarize, I will define three precise macros, and you can choose from them

#define IS_LVALUE(...) std::is_lvalue_reference<decltype((__VA_ARGS__))>::value
#define IS_XVALUE(...) std::is_rvalue_reference<decltype((__VA_ARGS__))>::value
#define IS_PRVALUE(...) !std::is_reference<decltype((__VA_ARGS__))>::value

Problem

With the following macro: ``` #define ASSERT_IF_TEMP(expr) static_assert(?, "Is temporary!"); ``` What should I put for question mark?

Original source