Search code examples
c++keywordname-lookupelaborated-type-specifier

When should I 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.

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?


Solution

  • 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.