views:

1041

answers:

2

I have this simple piece of code that uses boost::bind:

#include <boost/bind.hpp>
#include <utility>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<int> a;
    std::vector<std::pair<bool,int> > b;

    a.push_back(1);
    a.push_back(2);
    a.push_back(3);

    std::transform(a.begin(), a.end(), std::back_inserter(b),
                   boost::bind(std::make_pair<bool, int>, false, _1));
}

I'm getting a ton of errors in VS2010 RC, such as:

Error   1   error C2780: 'boost::_bi::bind_t<_bi::dm_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type> boost::bind(M T::* ,A1)' : expects 2 arguments - 3 provided c:\projects\testtuple\main.cpp  18  
Error   2   error C2780: 'boost::_bi::bind_t<Rt2,boost::_mfi::cmf8<R,T,B1,B2,B3,B4,B5,B6,B7,B8>,_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(boost::type<T>,R (__thiscall T::* )(B1,B2,B3,B4,B5,B6,B7,B8) const,A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 11 arguments - 3 provided   c:\projects\testtuple\main.cpp  18

Am I doing something wrong? If this is a bug in the compiler, how can I workaround it?

EDIT: added the entire test case.

Clarification: the code compiles in VS2008.

A: 

I think you just want std::bind1st(&std::make_pair, false) std::bind1st(std::ptr_fun(&std::make_pair<bool, int>), false))

MSalters
How would the new `transform` look? I tried `std::transform(a.begin(), a.end(), std::back_inserter(b), std::bind1st(` but it doesn't compile. Also what if I want to bind both of `make_pair` parameters?
Zack
I got it to work if I wrapped `make_pair` with `std::ptr_fun`
Zack
visitor
Can I use `bind1st` to bind both parameters? Suppose I have a `pair<int, int>` and I want both parameters to `make_pair` to get bound. Something similar to `boost::bind(std::make_pair<int, int>, _1, _1)`
Zack
Sorry - fixed. And no, I suggested `bind1st` precisely because your original problem was simple. `boost::bind` is far more powerful.
MSalters
+1  A: 

Update:

The problem is that make_pair seems to be overloaded in the STL that ships with VS2010 (it wasn't in previous versions of VS or in GCC). The workaround is to make explicit which of the overloads you want, with a cast:

#include <boost/bind.hpp>
#include <utility>
#include <vector>
#include <iterator>
#include <algorithm>


int main()
{
    std::vector<int> a;
    std::vector<std::pair<bool,int> > b;

    a.push_back(1);
    a.push_back(2);
    a.push_back(3);

    typedef std::pair<bool, int> (*MakePairType)(bool, int);

    std::transform(a.begin(), a.end(), std::back_inserter(b),
                    boost::bind((MakePairType)&std::make_pair<bool, int>,
                                false, _1));
}

For additional details see the Boost bind manual.

Manuel
No, still plenty of errors, now they're just coming from `std::bind`. `Error 1 error C2780: 'std::tr1::_Bind<_Ret,_Ret,std::tr1::_Bind0<std::tr1::_Callable_obj<_Fty>>> std::tr1::bind(_Fty)' : expects 1 arguments - 3 provided c:\projects\testtuple\main.cpp 17`
Zack
Maybe it's because `make_pair` is overloaded, can you try with a dummy `std::pair<bool, int> foo(bool,int)` function of your own instead of `make_pair`?
Manuel
it worked with a dummy function. but I have so many places using `std::make_pair`, I can't create a dummy function everywhere, do you have a suggestion?
Zack
Check the section "Binding an overloaded function" here: http://www.boost.org/doc/libs/1_42_0/libs/bind/bind.html#Troubleshooting
Manuel
`std::make_pair<bool, int>` is not overloaded.
MSalters
actually it is in VS2010. one version accepts a const reference and the other an r-value reference. I got it to work if I followed how to bind an overloaded function at `bind` docs. if you edit your answer to include it and mention that it's a workaround I'll accept it.
Zack