3

The original question is here: How to determine programmatically if an expression is rvalue or lvalue in C++? My question is, we can already distinguish between lvalue and rvalue, so can we go further and distinguish between xvalue and prvalue? My attempt is as follows:

#include <iostream>
#include <type_traits>

using namespace std;

#define IS_XVALUE(expr) (is_rvalue_reference<decltype((expr))>{})
#define IS_PRVALUE(expr) (!is_reference<decltype((expr))>{})
#define IS_LVALUE(expr) (is_lvalue_reference<decltype((expr))>{})

int main() {
  int a = 0;
  int &b = a;
  int &&c = 3;
  cout << IS_LVALUE(a);       // true
  cout << IS_LVALUE(b);       // true
  cout << IS_LVALUE(c);       // true
  cout << IS_PRVALUE(3);      // true
  cout << IS_XVALUE(move(a)); // true
}

It seems to be working correctly. But I'm not particularly confident. Is my approach correct? Is there a way to accomplish this without using macros?

colin
  • 49
  • 2

0 Answers0