Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested type as template parameter of base class

Tags:

c++

templates

Is it possible?

example:

template<class T>
class A {};

class B : public A<B::C>
{
public:
  struct C {};
};

Problem is that B::C is undeclared identifier (which is obvious why) and I don't know how to make it work. In summary: Can B derive from A with template parameter set to C?

like image 778
sluki Avatar asked Oct 08 '14 11:10

sluki


People also ask

Can a template class be a base class?

There is no requirement that your base class be a template. It is quite possible to have a template class inherit from a 'normal' class.

Can class template be nested?

Nested class templates are declared as class templates inside the scope of the outer class. They can be defined inside or outside of the enclosing class.

Can a template be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

Can we use non-type parameters as argument templates?

A non-type template argument provided within a template argument list is an expression whose value can be determined at compile time. Such arguments must be constant expressions, addresses of functions or objects with external linkage, or addresses of static class members.


1 Answers

No. B is incomplete at this point, because you have not yet defined the class it should inherit from. Thus it is not possible to reference B::C here (nested classes/structs depend on the complete definition of their enclosing class/struct, since the nested type could and often does depend on the definition of the enclosing one).

like image 180
dom0 Avatar answered Nov 11 '22 21:11

dom0