2015-04-05 4 views
1

Рассмотрим следующий пример:Как определить шаблон конструктора шаблона класса вне шаблона класса?

template<typename R> 
struct call { 
    template<typename F, typename... Args> 
    explicit call(F&& f, Args&&... args); 

    R result; 
}; 

template<typename R, typename F, typename... Args> 
call<R>::call(F&& f, Args&&... args) 
: result(std::forward<F>(f)(std::forward<Args>(args)...)) { } 

лязг кричит на меня:

 
utility.tpp:40:1: error: too many template parameters in template redeclaration 
template<typename R, typename F, typename... Args> 
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
utility.hpp:36:5: note: previous template declaration is here 
    template<typename R> 
    ^~~~~~~~~~~~~~~~~~~~ 

Я совершенно озадачен. Это вообще возможно?

ответ

5

Вам нужно два шаблона: один для класса и один для конструктора:

template <typename R>     // <== for call<R> 
template <typename F, typename... Args> // <== for call(F&&, Args&&...) 
call<R>::call(F&& f, Args&&... args)