I am putting together a "simple" template class. It offers an interface for performing some operations on a database, so there are other members as well (primarily for operating on the container member). However, for our purposes, the template class looks something like this:
template<typename T, //the type of objects the class will be manipulating
typename S> //the signature of the function the class will be using
FunctionHandler
{
private:
std::vector<T> container;
boost::function<S> the_operation;
SomeClass* pSC; //a database connection; implementation unimportant
//some other members--not relevant here
public:
boost::function<???> Operate;
FunctionHandler(boost::function<S> the_operation_)
: the_operation(the_operation_)
{
Operate = boost::bind(the_operation, pSC, std::back_inserter<std::vector<T> >,
/*infer that all other parameters passed to Operate
should be passed through to the_operation*/);
}
//other peripheral functions
}
My question is two-fold.
- What do I put as the template parameter for
Operate
. i.e. what replaces???
- How do I tell
boost::bind
that it should pass any other parameters given toOperate
on tothe_operation
? In other words, for some arbitrary function signatureS
that looks likevoid (SomeClass*, std::back_insert_iterator<std::vector<T> >, int, bool)
and some other arbitrary function signatureO
that looks likevoid (SomeClass*, std::back_insert_iterator<std::vector<T> >, double, double, bool)
how do I write this template class such thatOperate
has a signature ofvoid (int, bool)
for the first andvoid (double, double, bool)
for the second, and passes its values on tothe_operation
's 3rd-Nth parameters?
In my searches I couldn't find any questions quite like this one.
Why even use bind? We can get the same effect without it. I'm using
Iter
as a template but you can fill it in with whater the right type is:We're making all of the
operator()
s, but they'll only get instantiated if they get called - so as long as you call the right one (which for any solution, you'll have to anyway), this works.