Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a template with two parameters and two declarations of templates with one parameter each

Tags:

c++

templates

What would be the difference between the next two declarations:

template<class T, class functor>
methodReturnType className::methodName(functor f)

and:

template<class T>
template<class functor>
methodReturnType className::methodName(functor f)

I was trying to write a method that would work with a functor arg. The second declaration allowed me to avoid to declare the whole class as a template of both T and functor. I wanted to have a template class className of only one parameter T, but inside that class, a method had another parameter functor, while not declaring the whole class as a template of two parameters. It worked, but I didn't thoroughly understand it.

like image 401
Hélène Avatar asked Jul 08 '13 07:07

Hélène


1 Answers

Second variant is right for your case by language rules.

n3376 14.5.2/1

A member template of a class template that is defined outside of its class template definition shall be specified with the template-parameters of the class template followed by the template-parameters of the member template.

[ Example:

template<class T> struct string {
template<class T2> int compare(const T2&);
template<class T2> string(const string<T2>& s) { /∗ ... ∗/ }
};
template<class T> template<class T2> int string<T>::compare(const T2& s) {
}

— end example ]

like image 140
ForEveR Avatar answered Oct 02 '22 14:10

ForEveR