There are numerous example on SO regarding the use of placeholders however I am still a little bit confused and would appreciate it if someone could explain the difference between the following two statements
void SomeMethod(int a)
{
std::cout << "Parameter pass " << a << "\n";
}
Statement 1 : boost::bind(&SomeMethod,_1)(12);
Statement 2 : boost::bind(&SomeMethod,12)();
I believe I understand statement 1 which is chaining. The output of boost::bind(&SomeMethod,_1)
gets to have a parameter of 12 attached to it. However I have difficulty understanding whats happening in statement 2. If a parameter could be passed directly using boost::bind (as in statement 2) then why the need of a placeholder ?
One of uses of placeholders is to change the arguments order of a particular function
Example taken from boost . Assume you previously have
f(x, y)
andg(x,y,z)
. ThenIt is interesting to note the following BOOST guide statement about the last example
I think it is clear now that placeholders make this kind of replacement cleaner and more elegant.
In your particular case, the 2 uses are equivalent.
Another good use of placeholders is when you have lots of arguments and you only want to bind one of them. Example: imagine
h(a,b,c,d,e,f,g)
and you just want to bind the 6th argument!