由于boost的绑定功能,我有以下代码可以编译,以便在需要全局函数参数的情况下将非静态成员函数作为参数进行传递。注意,我省略了很多细节,但我的用例只是简单地传递一个非静态成员函数作为参数,并且我需要此函数的typedef,请参阅以下代码片段中的代码注释:
#include <boost/tuple/tuple.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <Eigen/Dense>
// ridge solver using conjugate gradient
template <>
inline void SomeANN<kConjugateGradient>::ridge_solve(const VectorXd& Y) {
// horrendous typedef I'd like to get rid of
typedef _bi::bind_t<tuples::tuple<double, VectorXd >,
_mfi::mf1<tuples::tuple<double, VectorXd>,
SomeANN<(Minimizer)1u>, const VectorXd&>, _bi::list2<_bi::value<SomeANN<(Minimizer)1u>*>,
boost::arg<1> > > oracle_f;
// I'd prefer this typedef instead of the ugly one above but doesn't compile
//typedef tuple<double, VectorXd> (SomeANN<kConjugateGradient>::*oracle_f)(const VectorXd&);
ConjugateGradient<BeginSpace, VectorXd, oracle_f> optimizer;
// ...
optimizer.search(BeginSpace(Y.rows()), boost::bind(&SomeANN<kConjugateGradient>::f, this, ::_1));
}
// definition of f. I need to pass this function as parameter to CG
template <>
inline tuple<double, VectorXd> SomeANN<kConjugateGradient>::f(const VectorXd& theta) {
// TODO: implement properly
double f = 0.0;
VectorXd df;
return make_tuple(f, df);
}但是我上面使用的从前面的错误消息中提取的typedef非常难看,我想使用更具可读性的东西,比如注释为typedef tuple<double, VectorXd> (SomeANN<kConjugateGradient>::*oracle_f)(const VectorXd&);的代码行,但它不能编译。为了能够在声明ConjugateGradient<BeginSpace, VectorXd, oracle_f> optimizer;中指定正确的模板参数,我需要一个oracle_f类型定义函数。
发布于 2013-05-31 00:04:05
使用boost::function怎么样?
typedef boost::function<tuples::tuple<double, VectorXd>(const VectorXd&)> oracle_f;发布于 2013-05-31 00:04:23
对不起,如果没有C++11的decltype或auto,您要么只能使用丑陋的大类型(实际上,这是Boost.Bind的一个实现细节),要么使用boost::function擦除类型,这会在每次调用时产生间接的开销。
搜索本身不是你上一个问题中的模板吗?
https://stackoverflow.com/questions/16840632
复制相似问题