我正在放在一起“简单”的模板类。它提供了一个用于在数据库上执行某些操作的界面,所以还有其他成员(主要用于在容器成员上操作)。但是,对于我们的目的,模板类看起来像这样:使用boost :: bind,但允许任何其他参数通过
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
}
我的问题是双重的。
- 作为
Operate
的模板参数,我该怎么做。即什么取代???
- 我该如何告诉
boost::bind
它应该通过其他参数Operate
到the_operation
?换句话说,对于一些任意的函数签名S
,看起来像void (SomeClass*, std::back_insert_iterator<std::vector<T> >, int, bool)
和看起来像void (SomeClass*, std::back_insert_iterator<std::vector<T> >, double, double, bool)
我怎么写这个模板类,使得Operate
有void (int, bool)
第一和void (double, double, bool)
第二签名一些其他任意函数签名O
,和将其值传递给the_operation
的第3个参数?
在我的搜索中,我找不到任何与此相似的问题。
应该如何编译器检查参数类型与函数参数类型兼容?它如何将参数类型的转换插入函数参数类型? – dyp
我不知道。回答这是回答问题的一部分,不是吗?预期的用途是在编译时和运行时参数类型都与参数类型匹配。 – caps
据我所知,任意类型的集合都是不可能的。当你在'boost :: function'中存储某些东西的时候,你可以删除所有关于它的信息(用于外部),并用一个接口(返回值+参数类型)替换它。 'boost :: function'似乎不支持省略号。你可以通过在'boost :: function'的函数签名中添加类似'void *'的东西来解决这个问题,然后通过这个指针传递附加参数。但是,这还不包括类型检查和转换。 – dyp