Jersey
Jersey

Reputation: 433

How to abbreviate class name inside its own definition?

How can I create an alias for a lengthy class name inside the definition of this class? I don't want it to be accessible outside the class definition. I can't get it to work with either typedef or using.

It's a class template in my case.

template<typename T>
class detailed_class_name{

typedef detailed_class_name<T> foo; // either one 
using foo = detailed_class_name<T>; // of these 

public:
    foo(); // Error: ISO C++ forbids declaration of 'foo' with no type. 
};

Any ideas?

Upvotes: 1

Views: 478

Answers (1)

Rakete1111
Rakete1111

Reputation: 48948

You cannot. A constructor's id-expression, i.e. the name of the constructor in a class definition is according to [class.ctor]p1 (emphasis mine):

  • in a member-declaration that belongs to the member-specification of a class but is not a friend declaration, the id-expression is the injected-class-name of the immediately-enclosing class;

And what is an injected-class-name? According to [class]p2

A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. [...]

And so the injected-class-name is the name of the class itself, and cannot be an alias.

This is further reinforced by this sentence in [class.ctor]p1:

The class-name shall not be a typedef-name.

Consider renaming the class if you don't even want to write a constructor because the name is too convoluted.

Upvotes: 2

Related Questions