May i use std::bind?


Hello. Can I use std::bind to create functions for chaiscript?

Simple code:

int Summator(int a, int b)
	return a + b;

int main()
	std::function<int(int)> Summator5 = std::bind(Summator, 5,std::placeholders::_1);
        int value = Summator5(2); //7

	chai.add(fun(&Summator5), "Sum5");

So, I get error inside chaiscript\dispatchkit\callable_traits.hpp (C2825 ‘T’: must be a class or namespace when followed by ‘::’ …)

If I’ll use lambda, i get the same

If I don’t need to use arguments of function in chaiscript I can wrap that function in lambda. But I cannot do it, if I want SOME argument (the same error),


chaiscript::fun supports binding.

chai.add(fun(&Summator, 5),"Sum5");


Two FYI’s on this:

Your first issue is that you were dereferencing the std::function object

// bad
chai.add(fun(&Summator5), "Sum5");
// good
chai.add(fun(Summator5), "Sum5");

The second is that there’s really never any good reason to use std::bind. I much better solution is to use a lambda (and by much better, I mean much much better. std::bind adds to compile size, compile time and runtime).

chai.add(fun([](int b){ return Summator(5, b); }, "Sum5");

See also:


Thanks , Jason, for your detailed answer.