mercredi 29 juillet 2015

Check if expression is xvalue or prvalue [duplicate]

This question already has an answer here:

I just want to verify some rules described at cppreference. It is easy to check, whether expression is lvalue or rvalue.

#include <cassert>
#include <iostream>

template <typename T>
bool IsLValue(const char* i_expression, T&, bool i_expected = true)
  {
  assert(i_expected);
  std::cout << "l_value : " << i_expression << std::endl;
  return true;
  }

template <typename T>
bool IsLValue(const char* i_expression, T&&, bool i_expected = false)
  {
  assert(!i_expected);
  std::cout << "r_value : " << i_expression << std::endl;
  return false;
  }

int main()
  {
  int i = 10;
  int* p = &i;

  IsLValue("The name of a variable 'i'", i);
  IsLValue("The name of a variable 'p'", p);
  IsLValue("The name of a function 'main' in scope", main);
  IsLValue("The name of a function 'std::cout' in scope", std::cout);
  IsLValue("Built-in pre-increment '++i'", ++i);
  //etc
  return 0;
  }

Is there any way to check if expression is xvalue or prvalue?

Aucun commentaire:

Enregistrer un commentaire