gpt4 book ai didi

c++ - boost::bind对应的类型定义

转载 作者:行者123 更新时间:2023-11-30 02:54:47 25 4
gpt4 key购买 nike

由于 boost 的绑定(bind)工具将非静态成员函数作为参数传递给需要全局函数参数的地方,我有以下代码可以编译。请注意,我省略了很多细节,但我的用例只是将非静态成员函数作为参数传递,我需要一个 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&);但它不会编译。我需要 oracle_f 的 typedef能够在声明中指定正确的模板参数 ConjugateGradient<BeginSpace, VectorXd, oracle_f> optimizer; .

最佳答案

抱歉,如果没有 C++11 的 decltypeauto,您只能使用丑陋的大类型(这是 Boost.Bind 的实现细节) ,真的),或者使用 boost::function 来删除类型,这会在每次调用时产生间接成本。

搜索本身不是您上一个问题中的模板吗?

关于c++ - boost::bind对应的类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16840632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com