由于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&);
,但它不会编译。我需要一个 typedef 以便oracle_f
能够在声明中指定正确的模板参数ConjugateGradient<BeginSpace, VectorXd, oracle_f> optimizer;
。