1

Is it possible to use two different types, one is a subtype of the other, with one template definition?

Something like:

template<typename T>
void foo(T a, T::bar b);
Petar Velev
  • 2,305
  • 12
  • 24

1 Answers1

2

You would need one more usage of typename

template <typename T>
void foo(T a, typename T::bar b);

because bar is a "dependent type" of T. See here for more details.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • So easy :), Can you define it as `template ` ? – Petar Velev Oct 22 '21 at 16:22
  • 1
    @PetarVelev: yes.. but it would not have identical meaning, and default would mostly be useless as `T2` would be deduced from argument. – Jarod42 Oct 22 '21 at 16:33
  • Yes. I know that the types can be deducted from the arguments. Maybe I could've used a better example. I was trying to make it as minimal as possible – Petar Velev Oct 22 '21 at 16:35