Search code examples
c++typedefselfno-op

Does a typedef to self have any effect?


I've come across some C++ code that has the following:

typedef Request Request;

Is this just a no-op or does this typedef actual have an effect, and if so, what effect does it have?


Solution

  • You can read all rules relative to typedef specifier for C++2003 ANSI ISO IEC 14882 2003 in section 7.1.3. In 7.1.3, 2) it have been said the identity typedef is allowed if the name already refers to some type.

    This is legal:

    typedef int Request;
    typedef Request Request; // Redefines "Request" with no effect 
    

    And that it is not:

    typedef Request Request; // Illegal, first "Request" doesn't name a type. 
    

    The standard has a specific example relating to this. C++2003, §7.1.3/2:

    In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers. [Example:

    typedef struct s { /* ... */ } s;
    typedef int I;
    typedef int I;
    typedef I I;
    

    end example]

    In 7.1.3, 3) it have been said that use typedef to redefine type to alias to another type is prohibited