C++: Is it defined behavior to typedef nested type with same name?

  c++, typedef

the code:

class Global
{
public:
    static const char *name() {return "Global";}
};
typedef Global T_Outter;

class Outter
{
public:
    typedef T_Outter O_Outter; // <= <1>
    typedef Outter T_Outter; // <= <2>
    static const char *name() {return "Outter";}
    static void test() {printf("self: %s, outter: %sn", name(), O_Outter::name());}

    class Inner
    {
    public:
        typedef T_Outter O_Outter;
        typedef Inner T_Outter;
        static const char *name() {return "Inner";}
        static void test() {printf("self: %s, outter: %sn", name(), O_Outter::name());}
    };
};

Outter::test();
Outter::Inner::test();
// output:
// self: Outter, outter: Global
// self: Inner, outter: Outter

what I want to do:

  • obtain nested class info without knowing it’s actual parent class or scope
  • the typedefs would be generated by macros, with the tricks, there’s no need to manually "bind" Outter and Inner‘s relationship, it’s done by typedef automatically

here’s the question:

  • is it defined behavior in the above code?
    • <1> and <2> with same T_Outter name
    • Outter::O_Outter equals to Global
    • Outter::Inner:O_Outter equals to Outter
  • is it portable to use the trick?

Source: Windows Questions C++

LEAVE A COMMENT