Home > Mobile >  Is there a way to limit the number of times we type template <typename T> in the cpp file?
Is there a way to limit the number of times we type template <typename T> in the cpp file?

Time:03-05

In my cpp file, before every function I need to include the line:

template <typename T>

Is there some short hand notation so that instead of typing this before every function, I can type the statement just once for a group of functions ? Also,

I also need to include after the class name, for example:

void MyClassName<T>::setDefaultValues()

Is there some way I can group all the template functions so I don't have to repeatedly type this for each function ?

CodePudding user response:

Nope. If you need it for each function, then the compiler needs to be told this directly before each function.

A slightly shorter way is of course template <class T>

And we could get even shorter if we resort to macros.

CodePudding user response:

Since this is a templated type, and you are on C 11, the consuming types are going to need the full definition anyway which means your other files will be #include ""-ing your source file.

Why not define everything in the header file like STL does for its containers and save you the trouble of redundant tokens and callers having to include files they don't really need to. Don't split the template into definition and implementation.

  • Related