Search code examples
c++c++11rvalue-referencervaluelvalue

Why is an enum variable an rvalue here?


Example:

typedef enum Color
{
    RED,
    GREEN,
    BLUE
} Color;

void func(unsigned int& num)
{
    num++;
}

int main()
{
    Color clr = RED;
    func(clr);
    return 0;
}

I get the following error when I compile this:

<source>: In function 'int main()':

<source>:16:9: error: cannot bind non-const lvalue reference of type 'unsigned int&' to an rvalue of type 'unsigned int'

     func(clr);

         ^~~

I think the variable (clr) I pass to func(unsigned int&) is an lvalue. I can get the address of clr and can assign another value to it. Why does it turn into an rvalue when I try to pass it to func(unsigned int&)?


Solution

  • clr itself is an lvalue of type Color. But the function does not accept a Color. It accepts a (reference to) unsigned int. So, the argument is converted (implicitly). And the result of the conversion is a prvalue of type unsigned int.