cli icon indicating copy to clipboard operation
cli copied to clipboard

Menu::Insert method does not work with free functions and with std::bind.

Open hanyukang opened this issue 4 years ago • 2 comments

Hi,

Thanks for this great work, One thing I've tried to achieve is to bind a member function to the Menu, but I'm getting an error message:

error C2661: 'cli::Menu::Insert': no overloaded function takes 5 arguments

void TestClass::Test(std::ostream& out)
{
	out << "test" << endl;
}

auto func = std::bind(&TestClass::Test, this, std::placeholders::_1);
rootMenu->Insert("test", func, "this is a test");

Am I doing something wrong? No matter how I tried always this error.

Is it possible to bind a member function instead of a lambda function?

Thanks

hanyukang avatar Jan 27 '21 23:01 hanyukang

@hanyukang thank you very much.

Currently, the Menu::Insert method does not work with std::bind (and unfortunately it does not work with free functions, either).

While I understand why free functions are not accepted (they don't have operator () as required by the Menu::Insert method), I must investigate deeply why std::bind is not accepted.

My idea would be to fix this issue (for both cases). Of course, in the meantime, you can wrap your method call in a lambda as a workaround:

rootMenu->Insert(
    "test", 
     [this](std::ostream& out)){ this->Test(out); }, 
     "this is a test"
);

If you like this library add a star (we're almost at 500 :-) ) and please consider becoming a sponsor to support our work by clicking the heart button at the top of the page :-)

daniele77 avatar Jan 28 '21 17:01 daniele77

Free function works with commit 63eb029

daniele77 avatar Jan 12 '22 16:01 daniele77