2015-04-05 178 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)