boost::bind doesn't work with boost::tuple::get<N>()

£可爱£侵袭症+ 提交于 2019-12-10 15:59:59

问题


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

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

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

回答1:


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).

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> ...

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

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




回答2:


Several problems here: eth_array not defined, I'm guessing that should be _array.

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

Here you are trying to call the tuple as a function? Maybe you tried something like:

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

Finally, what seems to be causing the issue you described:

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

You should try using a function pointer instead of raw function name:

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

The complete body of main() that I got to compile and run:

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


来源:https://stackoverflow.com/questions/7157210/boostbind-doesnt-work-with-boosttuplegetn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!