Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"too many template-parameter-lists" error when specializing a member function

I would like to define some template member methods inside a template class like so:

template <typename T>
class CallSometing {
public:
    void call (T tObj);  // 1st 

    template <typename A>
    void call (T tObj, A aObj); // 2nd 

    template <typename A>
    template <typename B>
void call (T tObj, A aObj, B bObj); // 3rd

};


template <typename T> void
CallSometing<T>::call (T tObj) {
    std::cout << tObj << ", " << std::endl;
}

template <typename T>
template <typename A> void
CallSometing<T>::call (T tObj, A aObj) {
    std::cout << tObj << ", " << aObj << std::endl;
}


template <typename T>
template <typename A>
template <typename B> void
CallSometing<T>::call (T tObj, A aObj, B bObj) {
    std::cout << tObj << ", " << aObj << ", " << bObj << ", " << std::endl;
}

But when instantializing the template class, there is an error concerning the three-argument menthod definition:

CallSometing<int> caller;

caller.call(12);  // OK
caller.call(12, 13.0); // OK
caller.call (12, 13.0, std::string("lalala!")); // NOK - complains "error: too many template-parameter-lists"

Could you please point what I am doing wrong? Why the (2nd) method is OK but the (3rd) causes a compile time error?

like image 372
Marcin Avatar asked Aug 29 '10 17:08

Marcin


People also ask

Which template can have multiple parameters?

Explanation of the code: In the above program, the Test constructor has two arguments of generic type. The type of arguments is mentioned inside angle brackets < > while creating objects.

What is the validity of template parameters inside the class inside that block only whole program?

What is the validity of template parameters? Explanation: Template parameters are valid inside a block only i.e. they have block scope. 4.


1 Answers

Please read a C++ template tutorial on how to give a template multiple parameters. Instead of

template<typename A> template<typename B> void f(A a, B b);

The way it is done is

template<typename A, typename B> void f(A a, B b);

Multiple template clauses represent multiple levels of templates (class template -> member template).

like image 159
Johannes Schaub - litb Avatar answered Oct 26 '22 01:10

Johannes Schaub - litb