Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ std::bind rebind function

Tags:

c++

c++11

bind

If I bind a function like this, using placeholders at the time of binding

std::bind(memberFunctionPointer, objectPointer, _1, _2);

Is it then possible to "rebind" it later to replace some / all of the placeholders, but without calling the function? I want to be able to pass in some parameters and then store it, to be invoked later on. (delayed callback)

like image 698
sangwe11 Avatar asked Nov 26 '14 23:11

sangwe11


People also ask

What does std :: bind do in C++?

std::bind is a Standard Function Objects that acts as a Functional Adaptor i.e. it takes a function as input and returns a new function Object as an output with with one or more of the arguments of passed function bound or rearranged.

How does STD bind work?

Internally, std::bind() detects that a pointer to a member function is passed and most likely turns it into a callable objects, e.g., by use std::mem_fn() with its first argument.

What is boost :: bind?

boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions.

What is the return type of std :: bind?

std::bind return type The return type of std::bind holds a member object of type std::decay<F>::type constructed from std::forward<F>(f), and one object per each of args... , of type std::decay<Arg_i>::type, similarly constructed from std::forward<Arg_i>(arg_i).


1 Answers

You can bind again:

auto f = std::bind(memberFunctionPointer, objectPointer, _1, _2);

auto g = std::bind(f, val1, val2);

g();   // (objectPointer->*memberFunctionPointer)(val1, val2)
like image 178
Kerrek SB Avatar answered Sep 18 '22 10:09

Kerrek SB