Search code examples
c++functiontemplateslambdadefault-parameters

Why doesn't passing a function with default parameters to a templatized constructor and storing it using a lambda as std::function<void()> work?


Take this simplified example:

Command.h:

class Command {
public:
    template<typename Func>
    Command(Func newToExecute) : toExecute([&newToExecute](){newToExecute();}) { }
    void callFunction(); //defined in Command.cpp -> calls function with toExecute();
private:
    std::function<void()> toExecute;
};

Main.cpp:

void test(int var = 0) {
    //does irrelevant things
}

int main() {
    Command testCommand(test);
    testCommand.callFunction();
    return 0;
}

When trying to run this I get an error from the compiler using MinGW: error: too few arguments to function Command(Func newToExecute) : toExecute([&newToExecute](){newToExecute();}) { }

Now I wouldn't have done all this just to save a simple void function, if this wasn't working:

//in the Command class:
Command(std::function<void()> newToExecute) : toExecute(std::move(newToExecute)) { } //rest unchanged
//in the main function:
Command testCommand([](){test();}); //rest unchanged

Coming from the last code example I see no reason, why the first one shouldn't work. Would anyone please explain why it is not working?

Edit: Missed a small detail in the working version, but still not closer to the explanation.


Solution

  • Your second version doesn't take the address of test. The type of that &test is void (*)(int), not void (*)().

    This is also why you can't construct a std::function<void()> from test directly.