1

We have a templated type Foo<T>. In the codebase, it's 99% instantiated with a couple of types, and I would like to use a distinct name for those cases.

What are the pros/cons of the using keyword (using FooA = Foo<A>) vs declaring an empty derived class (class FooA : public Foo<A> {};)

It seems that the derived class is smoother to use (allow forward declaration), but I wonder about any performance (speed or memory) cost.

maattdd
  • 181
  • 2
  • 6
  • 5
    An empty derived class is not the same type, as with using. So e.g. you can not bind a reference to Derived to an value of the template type. – gerum Nov 27 '22 at 11:30

1 Answers1

1

I would not use a derived class in this situation, unless you need to add additional data members or member functions to FooA.

All you are really doing here is creating an alias for a specific instantiation of the Foo<T> template. This is exactly what using is for, so using using makes your intent clear to anyone using type FooA. On the other hand, creating a derived class will add unnecessary complexity and confusion.

Dima
  • 38,860
  • 14
  • 75
  • 115