Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use an elaborated type specifier

Is there a particularly good reason to choose to use an elaborated type specifier? For example, in certain circumstances, one is required to use the template or typename keywords to disambiguate a dependent template or type.

But I can't think of any examples where this would occur for something such as an enumeration. Take the following code example:

enum Foo { A,  B };

void bar(Foo foo);
void baz(enum Foo foo);

Why might I choose to use the syntax baz() provides over bar() (or vice-versa)? Is there any ambiguous case?

like image 457
Shirik Avatar asked Jun 05 '12 18:06

Shirik


2 Answers

There are no reasons to use such specifiers, unless you are dealing with the situation when the name is hidden by name of a different "kind". For example, it is perfectly legal to declare a variable named Foo after the enum declaration, since, speaking informally, object names and type names live in independent "namespaces" (see 3.3/4 for more formal specification)

enum Foo { A, B };

int Foo;

After the int Foo declaration, your bar declaration will become invalid, while the more elaborate baz declaration will remain valid.

like image 93
AnT Avatar answered Nov 19 '22 16:11

AnT


Elaborated type specifiers are required for declaring user-defined types. One use case is to forward declare your types. In the unlikely event that you have a function with the same name as an enum you have visible in scope you may need to use the elaborated type specifier in the function declaration:

enum A { A_START = 0 };

void A(enum A a) {}

int main() {
   enum A a;
   A( a );
}
like image 3
dirkgently Avatar answered Nov 19 '22 15:11

dirkgently