【问题标题】:Can't use boost::bind to access boost::tuple elements不能使用 boost::bind 访问 boost::tuple 元素
【发布时间】:2011-07-26 04:54:28
【问题描述】:

我在 OSX.6 上使用 i686-apple-darwin10-gcc-4.2.1 编译器(我已经升级了 10.5 安装并继承了以前的开发工具包)。

我将这些库包含在我的实现中:

#include <vector>
#include <boost/bind.hpp>
#include <boost/tuple/tuple.hpp>

我有一个如下所示的元组:

typedef boost::tuple<double, unsigned long, unsigned long, char> myTupleType;

我有一个这样的元组向量:

std::vector<myTupleType> theTupleVector;

我有一个方法应该像这样检索值:

template <typename T,int i>
class accessTupleElement
{ 
public: 
    typedef typename boost::tuples::element<i,T>::type result_type;

    template <typename U> result_type operator()(const U& u) const
    { 
    // Select the ith element of the tuple T and return it 
    return boost::get<i>(u); 
    } 
};

然后我尝试做一些美妙的事情,例如:

std::find_if(startIter, endIter, boost::bind(std::equal<unsigned long>, boost::bind(accessTupleElement<myTupleType, 1>(), _1), 123L));

startIter 和 endIter 是我的元组向量的开始和结束。我假设这会将函数对象与迭代器的值绑定,评估为元组的所需值,并将其与 123 进行比较。

我得到的只是以下关于我的内部绑定的错误消息:

no matching function for call to 'bind(<unresolved overloaded function type>, boost::_bi::bind_t<boost::_bi::unspecified, accessTupleElement<boost::tuples::tuple<double, long unsigned int, long unsigned int, char, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, 1>, boost::_bi::list1<boost::arg<1> > >, boost::arg<2>&)'

我不明白,编译器怎么会混淆?只有一个名为 accessTupleElement 的标识符,我已经遵循了所有我能找到的试图解决这个问题的 boost 和 Internet 文档。这是如何解决的?

【问题讨论】:

  • 您确定问题出在您的内部绑定调用上,而不是外部绑定调用上吗? 看起来非常可疑,它指的是std::equal&lt;unsigned long&gt;
  • 你怎么认为它指的是 std::equal?我读错了吗?
  • @Matthew 因为第二个参数(boost::_bi::bind_t)的类型看起来像内部bind的返回类型。

标签: c++ boost bind tuples


【解决方案1】:

改变

std::equal<unsigned long>

std::equal<unsigned long>()

好吧,std::equal 是一个函数模板,因此是错误消息。您可能想要std::equal_to,因此请使用以下内容:

std::equal_to<unsigned long>()

【讨论】:

  • 你能解释一下为什么第一个错误,第二个是正确的吗?
  • 不,“没有对 'equal()' 的匹配函数调用”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 2012-04-18
  • 2016-03-16
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多