I recently found this nifty snippet on the web - it allows you to bind without having to pass in explicit placeholders:
template
This was troubling me a lot, since I had to bind a function in a situation when I did not know the arguments at the time. (A factory such as shown here How to implement serialization in C++)
For example (assume TSubClass::create is static)
template
class Factory
{
public:
template
void register(int id)
{
_map.insert(std::make_pair(id, std::bind(&TClass::create, /*how to give TArgs as placeholders??*/)));
}
}
instead I was able to replace the std::bind with a lambda expression without having to use all these helper classes!
template
class Factory
{
public:
template
void register(int id)
{
_map.insert(std::make_pair(id, [](TArgs... args) { TSubClass::create(args...); }));
}
}
as a bonus, you can also "bind" to constructors with this mechanism