c++ - 如何使用 Boost::Phoenix 传递函数参数?

标签 c++ boost boost-phoenix

这是我在这里的第一篇文章,如果我不尊重“风俗习惯”,请多多包涵:)

我是 Boost::Phoenix 的新手,我想将一个函数传递给定义如下的方法:

template <typename Selector>
Result Greedy ( 
    const t_Capacity& capacity,
    BTSSet stations,                
    BTSSet startSet)               
{
  //...

  function <Selector> sel;
  while ( !stations.empty() ) { 
    BTSSet::iterator currentStation = sel(stations);

    // ...
    }
// ...
}

我的选择器函数是:

struct rouletteWheelSelector {
  typedef BTSSet::iterator result_type;

  BTSSet::iterator operator () ( const BTSSet& stations ) { 
    // ...
  }
};

但我的编译器说无法从 from 'typename detail::expression::function_eval<rouletteWheelSelector, set<BTS *, BTS_Cmp, allocator<BTS *> > >::type const' 转换到 BTSSet::Iterator。

我的仿函数声明可以吗?我怎样才能强制编译器推断出 sel 的正确返回类型?

谢谢!

最佳答案

你有三个问题:

  1. boost::phoenix::function<>是惰性的,因此必须对其进行两次评估才能获得实际结果。
  2. rouletteWheelSelector::operator()必须是 const 才能被 boost::phoenix::function<> 使用.
  3. sel正在捕获 stations按值,然后将迭代器返回到已销毁的集合中;使用 boost::phoenix::cref捕获stations通过常量引用。

此代码使用 VC++ 2010 SP1 和 Boost 1.47.0 为我编译和运行干净:

#include <memory>
#include <set>
#include <boost/phoenix.hpp>

struct BTS
{
    explicit BTS(int const val_) : val(val_) { }
    int val;
};

struct BTS_Cmp
{
    typedef bool result_type;

    bool operator ()(BTS const* const a, BTS const* const b) const
    {
        if (a && b)
            return a->val < b->val;
        if (!a && !b)
            return false;
        return !a;
    }
};

typedef std::set<BTS*, BTS_Cmp> BTSSet;

struct rouletteWheelSelector
{
    typedef BTSSet::iterator result_type;

    BTSSet::iterator operator ()(BTSSet const& stations) const
    {
        return stations.begin();
    }
};

template<typename Selector>
void Greedy(BTSSet stations)
{
    namespace phx = boost::phoenix;

    phx::function<Selector> sel;
    while (!stations.empty())
    {
        BTSSet::iterator currentStation = sel(phx::cref(stations))();
        std::auto_ptr<BTS> deleter(*currentStation);
        stations.erase(currentStation);
    }
}

int main()
{
    BTSSet stations;
    stations.insert(new BTS(1));
    stations.insert(new BTS(2));
    stations.insert(new BTS(3));
    stations.insert(new BTS(4));
    stations.insert(new BTS(5));
    Greedy<rouletteWheelSelector>(stations);
}

如果您使用的是 Phoenix v2 而不是 Phoenix v3,正如 @jpalecek 在他现已删除的答案中正确指出的那样,您必须使用嵌套的 result<> rouletteWheelSelector 内的模板而不是 result_type :

struct rouletteWheelSelector
{
    template<typename>
    struct result
    {
        typedef BTSSet::iterator type;
    };

    BTSSet::iterator operator ()(BTSSet const& stations) const
    {
        return stations.begin();
    }
};

但是,综上所述,您为什么要使用 boost::phoenix::function<>在这里吗?为了您的使用,Greedy<>没有它可以更容易(和有效地)实现:

template<typename Selector>
void Greedy(BTSSet stations)
{
    Selector sel;
    while (!stations.empty())
    {
        BTSSet::iterator currentStation = sel(stations);
        std::auto_ptr<BTS> deleter(*currentStation);
        stations.erase(currentStation);
    }
}

关于c++ - 如何使用 Boost::Phoenix 传递函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7759508/

相关文章:

c++ - OpenGL 渲染(仅实时)?

c++ - 为什么这个 while 循环永远不会评估为真?

c++ - 这是什么数组声明?

c++ - 在 C++ 中使用 'boost::system::error_code'

c++ - 使用 phoenix 访问 boost::tuple 的简单方法

c++ - 如何让这个链表堆栈实现在 C++ 中运行?

c++ - 如何在 Boost 程序选项中获得更好的错误消息

c++ - 对 boost::filesystem::path_traits::convert 的 undefined reference

c++ - 尝试使用 Phoenix Bind 计算字符数时出现 boost.spirit "invalid static_cast"错误

c++ - 如何在语义操作中访问元组的元素