zennehoy
zennehoy

Reputation: 6846

"Overloading" templated and non-templated types

In c++, it is possible to overload a templated function such that it can be called with template parameters or without:

void func();
template <typename T> void func();

func();
func<int>();

Is the same possible for a Type (i.e., class)?

class Class;
template <typename T> class Class;

Class a;
Class<int> b;

I am only interested in being able to use both Class and Class<T> as types, where Class would behave identically to Class<void> - the declaration of the classes can be as complex as necessary to get this to work.

Upvotes: 3

Views: 71

Answers (1)

pmr
pmr

Reputation: 59811

Add a default argument.

template<typename T = void>
class Class;

Class<> is now equivalent to Class<void>.

Upvotes: 2

Related Questions