James Dean
James Dean

Reputation: 83

Enum can be passed only by right reference

I would kindly ask someone to explain me a bit why an enum can only be passed by right reference and not as simple reference ?

 namespace n1
{
    enum e1
    {
        f = 1,
        f1
    };
}
void f(n1::e1&& e)
{
    std::cout<<static_cast<int>(e);

}
int main()
{
   f(n1::e1::f1);
    return 0;
}

Upvotes: 1

Views: 58

Answers (2)

Sambalika
Sambalika

Reputation: 100

Enumerations are integral constants :

  • an enum parameter of a function must be constant

Remark : you don't have to cast the enum to print his value


void f(const n1::e1& e) {
    std::cout<< e ;
}

Upvotes: 0

Ted Lyngmo
Ted Lyngmo

Reputation: 117298

enums are constants so you can't take it as n1::e1& but as const n1::e1&:

void f(const n1::e1& e) {
    std::cout<<static_cast<int>(e);
}

Upvotes: 3

Related Questions