3

I have the following code, which defines a variadic template taking std::function as parameter:

#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);
}

This does not compile neither on GCC 4.9, 5.4, 6.2, nor on Clang 3.8. The error message is always something similar to

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&)>’

The problem disappears, as soon as I remove the extra argument Dummy& in the signature of std::function in both cases. Does anybody have an idea, what is going on here? I would expect the version with the Dummy argument to compile as well.

Martin
  • 1,261
  • 7
  • 11

0 Answers0