Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple template functions inside non-template class

Tags:

c++

templates

I have a class foo which contain two template functions Add() and Subtract().

struct foo
{
    template <typename U>
    U* Add();
    template <typename U>
    U* Subtract();
};

Is it correct to use same template parameter U for both of them? Also do I need to write template <typename U> every time before a template function declaration?

like image 422
rajiv Avatar asked Mar 07 '23 08:03

rajiv


1 Answers

Yes, you can use the same name for template parameters in different functions, the same way you can name arguments the same. Those names in different functions are completely unrelated.

And yes, you have to use keyword template as per C++ grammar.

like image 144
SergeyA Avatar answered Mar 11 '23 20:03

SergeyA