如何在 class 模板之外定义 class 模板的构造函数模板?
How to define a constructor template of a class template outside of the class template?
考虑以下几点:
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>
^~~~~~~~~~~~~~~~~~~~
我非常困惑。有可能吗?
您需要两个 模板:一个用于class,一个用于构造函数:
template <typename R> // <== for call<R>
template <typename F, typename... Args> // <== for call(F&&, Args&&...)
call<R>::call(F&& f, Args&&... args)
考虑以下几点:
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> ^~~~~~~~~~~~~~~~~~~~
我非常困惑。有可能吗?
您需要两个 模板:一个用于class,一个用于构造函数:
template <typename R> // <== for call<R>
template <typename F, typename... Args> // <== for call(F&&, Args&&...)
call<R>::call(F&& f, Args&&... args)