0

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.