用std :: function调用C ++ 11 variadic函数

时间:2016-11-09 13:05:49

标签: c++ c++11 variadic-templates

我有以下代码,它定义了一个以std :: function作为参数的可变参数模板:

#include <string>
#include <functional>

struct Dummy;

template<typename ResultType, typename ...ArgTypes>
void addFuncDef(const std::string& funcName, const std::function<ResultType (ArgTypes..., Dummy&)>&)
{}

int test(void)
{
    std::function<double(double,Dummy&)> func;
    addFuncDef(std::string("test"), func);
}

这不能在GCC 4.9,5.4,6.2或Clang 3.8上编译。错误消息总是类似于

test.cpp:18:40: error: no matching function for call to ‘addFuncDef(std::__cxx11::string, std::function<double(double, Dummy&)>&)’
addFuncDef(std::string("test"), func);
                                    ^
test.cpp:10:6: note: candidate: template<class ResultType, class ... ArgTypes> void addFuncDef(const string&, const std::function<ResultType(ArgTypes ..., Dummy&)>&)
void addFuncDef(const std::string& funcName, const std::function<ResultType (ArgTypes..., Dummy&)>&)
     ^
test.cpp:10:6: note:   template argument deduction/substitution failed:
test.cpp:18:40: note:   mismatched types ‘Dummy&’ and ‘double’
    addFuncDef(std::string("test"), func);
                                    ^
test.cpp:18:40: note:   ‘std::function<double(double, Dummy&)>’ is not derived from ‘const std::function<ResultType(ArgTypes ..., Dummy&)>’

一旦我删除额外的参数Dummy&amp;在两种情况下都在std :: function的签名中。有没有人有想法,这里发生了什么?我希望带有Dummy参数的版本也可以编译。

0 个答案:

没有答案