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.
Operate` such that you can store various functions in it that have different signatures (like `void(SomeClass*, some_type, int, bool)` and `void(SomeClass*, some_type, double, double, bool)`; and any additional arguments passed to `Operate` should be forwarded to the stored function.– dyp Dec 20 '14 at 19:56