Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value category of member enumerator

Tags:

c++

c++11

According to cppreference.com a.m is prvalue the member of object expression, where m is a member enumerator or a non-static member function[2];

Does it mean for object expression ie a.m where m is a member of a of enumeration type, the value category of expression itself would be prvalue?

As per my following testing, it does not seem to be the case. What did I miss?

enum class Color : char {
    Red = 'r'
};

struct Test {
    Color c;
};

void func(Color&&) {
    std::cout << "rvalue ref\n";
}

void func(Color&) {
    std::cout << "lvalue ref\n";
}

int main()
{
    Test t;
    func(t.c);
    return 0;
}

Output: lvalue ref

like image 552
cpp Avatar asked Sep 19 '25 08:09

cpp


1 Answers

So the 'enumerator' is the value of an enumeration not an object of the type of the enumeration: https://en.cppreference.com/w/cpp/language/enum

An enumeration is a distinct type whose value is restricted to a range of values, which may include several explicitly named constants ("enumerators").

Slightly edited to remove fluff, bolding mine

So the standard is referring to when you access a class enumerator like:

struct Test {
   enum Colour {
     Red
   };
};

...

Test t;
t.Red; // <- This is a prvalue

Here is a live example.

like image 68
Fantastic Mr Fox Avatar answered Sep 21 '25 00:09

Fantastic Mr Fox