2

The code in question:

boost::function<bool()> isSpecialWeapon = boost::bind(&WeaponBase::GetType,this) == WeaponType::SPECIAL_WEAPON;

The error I get is something like so:

 undefined reference to `boost::_bi::bind_t<bool, boost::_bi::equal, 
 boost::_bi::list2<boost::_bi::bind_t<WeaponType::Guns, 
 boost::_mfi::cmf0<WeaponType::Guns, WeaponBase>,  
 boost::_bi::list1<boost::_bi::value<WeaponBase*> > >, 
 boost::_bi::add_value<WeaponType::Guns>::type> > boost::_bi::operator==
 <WeaponType::Guns, boost::_mfi::cmf0<WeaponType::Guns, WeaponBase>, 
 boost::_bi::list1<boost::_bi::value<WeaponBase*> >, WeaponType::Guns>
 (boost::_bi::bind_t<WeaponType::Guns, boost::_mfi::cmf0<WeaponType::Guns, WeaponBase>, 
 boost::_bi::list1<boost::_bi::value<WeaponBase*> > > const&, WeaponType::Guns)'
dchhetri
  • 6,926
  • 4
  • 43
  • 56
  • What are you trying to accomplish, exactly? – Emile Cormier May 02 '13 at 18:57
  • @EmileCormier Trying to create a function that determines if the current object is of type X given that it can be of type {X,Y,Z} – dchhetri May 02 '13 at 18:59
  • Are you willing/able to use C++11 features? – Emile Cormier May 02 '13 at 19:02
  • @EmileCormier Sorry the system I am working on does not support C++11, else I would have used lamdas. Can you tell me if I am reading the documentaion wrong http://www.boost.org/doc/libs/1_53_0/libs/bind/bind.html#operators – dchhetri May 02 '13 at 19:04
  • What compiler are you using? From the documentation, it appears that it should work. In that a bound functor compared to a value should return a new bind functor which does the comparison. – Dave S May 02 '13 at 19:12
  • "gcc version 4.4.4 20100726 (Red Hat 4.4.4-18) (GCC)" is the underlying compiler I belive – dchhetri May 02 '13 at 19:16
  • I couldn't get it working either with VS2010. – Emile Cormier May 02 '13 at 20:18
  • Ok, `boost::bind(...) == constant` is working for me now in VS2010. I must be losing my mind... Sorry about the confusion. – Emile Cormier May 02 '13 at 20:27

1 Answers1

1

If you can't get boost::bind to work as you desire, you can try Boost.Pheonix or Boost.Lamda as a workaround.

Try using boost::pheonix::bind (from Boost.Pheonix) instead of boost::bind:

#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/bind/bind_member_function.hpp>
#include <boost/function.hpp>
#include <iostream>

enum WeaponType {melee, ranged, special};

class Sword
{
public:
    WeaponType GetType() const {return melee;}

    void test()
    {
        namespace bp = boost::phoenix;
        boost::function<bool()> isSpecialWeapon =
            bp::bind(&Sword::GetType, this) == special;
        std::cout << "isSpecialWeapon() = " << isSpecialWeapon() << "\n";
    }

};

int main()
{
    Sword sword;
    sword.test();
}

Alternatively, you also use boost::lambda::bind (from Boost.Lambda):

#include <boost/function.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <iostream>

enum WeaponType {melee, ranged, special};

class Sword
{
public:
    WeaponType GetType() const {return melee;}

    void test()
    {
        boost::function<bool()> isSpecialWeapon =
            boost::lambda::bind(&Sword::GetType, this) == special;
        std::cout << "isSpecialWeapon() = " << isSpecialWeapon() << "\n";
    }

};

int main()
{
    Sword sword;
    sword.test();
}
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
  • I see, I was under the impression that the first statement in your code, produces a boost function instead of a boolean value. Am I reading the documentation wrong http://www.boost.org/doc/libs/1_53_0/libs/bind/bind.html#operators – dchhetri May 02 '13 at 19:00
  • @user814628: I didn't know bind had support for operators. I've got something working with boost::phoenix and have updated my answer. – Emile Cormier May 02 '13 at 19:44
  • @user814628: Got it working with both Boost.Pheonix and Boost.Lambda. Enjoy! :-) – Emile Cormier May 02 '13 at 20:13
  • Awesome, its weird, I have to make sure my code works for two different platforms, the original code compiles in one platform but fails with the given error in the other. I want my code to be uniform, so so instead of having ifdefs for different platforms, I just created a local function instead of using the inline bind and that seems to work on both platforms. Thank you for your phoenix and lamda implementation. I will have to look into those namespace and see what's different or better about their bind. – dchhetri May 03 '13 at 03:52