Lets say I have vectors of different objects, say D is of type vector< Dog > and R is of type vector< Rock >. Is it possible to use templates so I can automatically have all member functions be callable on the vectors? I.e. if Dog has a function Bark(), I want D.bark() to cause all the dogs to bark.
Now, clearly I could code this behavior by hand with a new function and a simple loop, but is
there an easy way to template it? I.e. so that R.smash() smashes all the rocks without me
ever explicitly coding that loop. I’m trying to imagine what the syntax would be and it is blowing my mind.
In C++03, you can use
boost::bindinstead. You can try to wrap it in a separate function template, but I have a feeling it won’t buy you much. Usingfor_eachwhere you need this behaviour is clear enough.