0

Can i write code like this

class Foo
{
    typedef Foo type;
};

I don't see any limitations in standard about this.

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

2 Answers2

1

NOTE: Compiler: Apple GCC, OS: OSX 10.7

GCC finds nothing wrong with this, and I am able to use the type type later in the definition of Foo. However, you cannot use the type type outside of Foo.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
0

It is possible to write that code. type and Foo are not reserved words, and C++ allows to define types within structs or classes. In fact, they are heavily used in templates for metaprogramming and type introspection. See How do you understand dependent names in C++ for further details on that aspect of the language.

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52