boost::bind 不适用于 boost::tuple::get<N>() [英] boost::bind doesn't work with boost::tuple::get<N>()

查看:20
本文介绍了boost::bind 不适用于 boost::tuple::get<N>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use boost::bind and STL with boost::tuple, but each time I try to compile I get the following error.

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

你知道我在这里做错了什么吗?为什么只针对 boost::arg<1>?

Do you know what I am doing wrong here and why is only for the boost::arg<1>?

谢谢AFG

    #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 )
                     )
            );

推荐答案

get 函数有不止一个模板参数:除了索引,还在元组的内容上参数化(cons 的头部和尾部).

The get function has more than one template parameter: in addition to the index, it is also parameterized on the content of the tuple (the head and the tail of the cons).

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

Consequently, get<0> is not an instantiation of the template; you need to provide the additional arguments:

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

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

然而,这仍然不起作用,因为 get 被重载(const 和非常量版本),所以你需要明确说明你想要的重载.为此,您需要使用具有正确类型的函数指针:

However, this still won't work because get is overloaded (const and non-const version), so you need to explicitely state which overload you want. To do so, you need to use a function pointer with the correct type:

// 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>;

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

Now you can use these function pointers in your bind expression:

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 并不能很好地配合...

As you can see, overloaded function templates and bind does not get along very well...

这篇关于boost::bind 不适用于 boost::tuple::get&lt;N&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆