Using class type in class?

M

mlt

I have some code that looks like this:

template<typename math>
class FirstClass {

public:
typedef math math_types;
typedef FirstClass<math_types> types;
...
typedef SecondClass<types> second_type;
...

}


How can a class typedef itself (FirstClass) and be used as a template
parameter for another type (SecondClass)? Don't I need to define
FirstClass before I can use it as a type parameter for SecondClass?
 
K

Kai-Uwe Bux

mlt said:
I have some code that looks like this:

template<typename math>
class FirstClass {

public:
typedef math math_types;
typedef FirstClass<math_types> types;
...
typedef SecondClass<types> second_type;
...

}


How can a class typedef itself (FirstClass) and be used as a template
parameter for another type (SecondClass)? Don't I need to define
FirstClass before I can use it as a type parameter for SecondClass?

In principle, you don't need to define a type before it can be used as a
template parameter: in other words, you can use incomplete types as
template parameters. In that case, however, the program has undefined
behavior if completing the type changes the semantics (and there might be
some more, technical restrictions). In any case, the compiler is not
required to issue a diagnostic.

Also, the templates from the standard library can only be instantiated from
complete types. Otherwise, you have undefined behavior.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top