C++ enum keyword in function parameters

195 views Asked by At

What is the point of using the enum keyword in the function parameter? It seems to do the same thing without it.

enum myEnum{
  A, B, C
};

void x(myEnum e){}

void y(enum myEnum e){}

Is there a difference between the two?

1

There are 1 answers

1
Vlad from Moscow On BEST ANSWER

In this function declaration

void x(myEnum e){}

the enumeration myEnum shall be already declared and not hidden.

In this function declaration

void y(enum myEnum e){}

there is used the so-called elaborated type name. If in the scope there is declared for example a variable with the name myEnum like

int myEnum;

then using this function declaration

void y(enum myEnum e){}

allows to refer to the enumeration with the name myEnum that without the keyword enum would be hidden by the declaration of the variable.

Here is a demonstrative program.

#include <iostream>

enum myEnum{
  A, B, C
};

void x(myEnum e){}

int myEnum; 

//  compiler error
//void y(myEnum e){} 

void y(enum myEnum e){}

int main() {
    // your code goes here
    return 0;
}

As it is seen the commented function declaration will not compile if to uncomment it.