3

I'm confused about how to tell apart "lvalue" and "rvalue" in C, and "lvalue", "xvalue", "prvalue" in C++.

I'm thinking about getting the value type by overload in C++.

#include <iostream>

template<typename T>
void value_category(T & var)
{
    std::cout << "Left Value" << std::endl;
}

template<typename T>
void value_category(T && var)
{
    std::cout << "Right Value" << std::endl;
}

int main(void)
{
    int arr1[4];
    int arr2[4];
    int arr3[4];
    int * mat[3] = {arr1, arr2, arr3};

    value_category(arr3);
    value_category(arr2[3]);
    value_category(mat);
    value_category(mat[0]);
}

But it only tells whether lvalue or rvalue? Are there other solutions?

Tinyden
  • 524
  • 4
  • 13
  • 1
    You are getting confused on multiple levels. Reference and Rvalue reference types are not the same things as lvalue or rvalue or prvalue or xvalue or glvalue (which are value categories). Overloads are based on types, not on value categories. It is not possible to detect value categories by overloading. Also in this context `T && var` is so-called "universal reference" which is not nessesery the same as rvalue reference because of the funky rules of template parameter deduction. – user7860670 Sep 28 '19 at 20:37
  • 2
    Possible duplicate of [How to determine programmatically if an expression is rvalue or lvalue in C++?](https://stackoverflow.com/questions/36296425/how-to-determine-programmatically-if-an-expression-is-rvalue-or-lvalue-in-c) – ph3rin Sep 28 '19 at 21:01
  • @VTT technically it's called forwarding reference, not universal reference. – Timo Sep 28 '19 at 21:04
  • Use `decltype` with a parenthesized expression and determine whether the resulting type is a `T`/`T&`/`T&&`. – L. F. Sep 29 '19 at 06:07

0 Answers0