【问题标题】:boost::bind doesn't work with boost::tuple::get<N>()boost::bind 不适用于 boost::tuple::get<N>()
【发布时间】:2011-08-23 06:44:44
【问题描述】:

我正在尝试将boost::bind 和STL 与boost::tuple 一起使用,但每次尝试编译时都会出现以下错误。

      error: call of overloaded ‘bind(<unresolved overloaded function type>, 
      boost::arg<1>&)’ is ambiguous

你知道我在这里做错了什么吗?为什么只针对boost::arg&lt;1&gt;

谢谢 自动对焦

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <cstdio>
    #include <boost/tuple/tuple.hpp>
    #include <boost/assign.hpp>
    #include <boost/bind.hpp>

    int main( int argc, const char** argv ){


            using namespace boost::assign;
            typedef boost::tuple< int, double > eth_array;

            std::vector< eth_array > v;
            v+= boost::make_tuple( 10,23.4), boost::make_tuple( 12,24.4) );
            std::for_each( v.begin()
                    , v.end()
                    , boost::bind<int>(
                            printf
                            , "%d-%f"
                            , boost::bind( eth_array::get<0>, _1 )
                            , boost::bind( eth_array::get<1>, _1 )
                     )
            );

【问题讨论】:

  • 什么是eth_arrayget 不是 boost 命名空间中的东西吗?不是这个eth_array 命名空间/类?
  • auch ..我在复制代码时已更改。已在上述示例中修复。

标签: c++ stl boost-bind boost-tuples


【解决方案1】:

get函数有多个模板参数:除了索引之外,还对元组的内容(cons的头部和尾部)进行了参数化。

因此,get&lt;0&gt; 不是模板的实例化;您需要提供额外的参数:

typedef eth_array::head_type head;
typedef eth_array::tail_type tail;

... get<0, head, tail> ...

但是,这仍然行不通,因为 get 已重载(常量和非常量版本),因此您需要明确说明您想要的重载。为此,您需要使用具有正确类型的函数指针:

// const version of get, which takes and returns const references
int const & (*get0)( boost::tuples::cons<head, tail> const & ) = 
    boost::get<0, head, tail>;
double const & (*get1)( boost::tuples::cons<head, tail> const & ) = 
    boost::get<1, head, tail>;

现在您可以在绑定表达式中使用这些函数指针:

std::for_each( v.begin(),
               v.end(),
               boost::bind<int>(
                   printf,
                   "%d-%f",
                   boost::bind( get0, _1 ),
                   boost::bind( get1, _1 )
               )
);
// outputs 10-23.40000012-24.400000

如你所见,重载的函数模板和bind 相处得不是很好...

【讨论】:

  • 嗨,卢克!这是一个超级回应。从未对 boost::tuple + 模板有如此深入的了解!
【解决方案2】:

这里有几个问题: eth_array 未定义,我猜应该是_array。

v+= ( boost::make_tuple( 10,23.4) )( boost::make_tuple( 12,24.4) );

您在这里尝试将元组作为函数调用?也许您尝试过类似的方法:

v+=boost::make_tuple( 10,23.4);
v+=boost::make_tuple( 12,24.4);

最后,似乎是什么导致了您描述的问题:

boost::bind( eth_array::get<0>, _1 )

您应该尝试使用函数指针而不是原始函数名:

boost::bind( &eth_array::get<0>, _1 )

我要编译和运行的 main() 的完整主体:

int main( int argc, const char** argv ){

    using namespace boost::assign;
    typedef boost::tuple< int, double > _array;

    std::vector< _array > v;
    v+=boost::make_tuple( 10,23.4);
    v+=boost::make_tuple( 12,24.4);
    std::for_each( v.begin()
            , v.end()
            , boost::bind<int>(
                    printf
                    , "%d-%f\n"
                    , boost::bind( &_array::get<0>, _1 )
                    , boost::bind( &_array::get<1>, _1 )
             )
    );
}

【讨论】:

  • 你好,AndrzejJ。你是对的我的意思是 v+= boost::make_tuple( 10,23.4 ) , boost::make_tuple( 12,24.4 ); (我做了一些乱七八糟的事情,我修复了我的代码以使其更具可读性)。关键是 boost::bind( &eth_array::get, _1 ) 在 std::for_each 中仍然不起作用,即使使用 &。仍然只适用于 arg
  • 您的代码中没有定义 eth_array 类型,但是当我使用 _array 时,我得到了它的编译和运行。我编辑了答案以包含在我的系统上编译的整个 main() 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2011-07-11
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多