c++ - 如何在类模板之外定义类模板的构造函数模板?

标签 c++ templates syntax

<分区>

考虑以下几点:

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)...)) { }

clang 对我大喊:

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>
    ^~~~~~~~~~~~~~~~~~~~

我真的很困惑。有可能吗?

最佳答案

您需要两个模板:一个用于类,一个用于构造函数:

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

关于c++ - 如何在类模板之外定义类模板的构造函数模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29458582/

相关文章:

c++ - 使用模板删除/添加结构成员

syntax - Knockout.js 无容器 "foreach"不适用于 <table>

scala - "for (i <- 1 to x; j <- 1 to y)"实际上如何增加 Scala 中的变量?

python - 函数声明中单独的星号 * 是什么意思?

c++ - 将 .pch 文件与 libclang api 一起使用

c++ - 创建远程线程会使进程崩溃

c++ - 将十进制转换为 32 位二进制?

c++ - Pthread 模板参数错误

c++ - 无法遍历模板列表 C++

c++ - 1>项目: error PRJ0003 : Error spawning 'rc.exe'