Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for C++ subclasses to share the same template?

Is it possible for subclasses to share the same template? For example:

template <class T>
class A
{
private:
  T _aObj;
public:
  class B
  {
  public:
    T _bObj;
  };
};

Where T can be used in both A and B?

When I've tried this, I get the following error:

error: A::B is not a template

like image 984
poy Avatar asked Feb 19 '23 10:02

poy


1 Answers

Yes that works fine (on a standards-complying compiler).

A way of thinking why this is logical is because B is not simply part of A, it is part of A<T>! T is not only part of the type for A, but also for B (the correct name for it would be A<T>::B.)

like image 163
orlp Avatar answered Feb 21 '23 22:02

orlp