Boost::Bind strange compilation errors

Discussion in 'C++' started by P G, Aug 8, 2004.

  1. P G

    P G Guest

    I hope this is on topic here. I have a problem compiling a simple
    example of the use of boost::bind. Please take a look at the program
    below.


    /**************************************************************************/

    #include <boost/function.hpp>
    #include <boost/bind.hpp>
    #include <boost/shared_ptr.hpp>
    #include <iostream>
    #include <memory>

    using namespace boost;

    struct X{

    void f()
    {
    std::cout<<"Bind Test\n";
    }
    };

    int main()
    {

    X x;

    shared_ptr<X> p(new X());

    bind(&X::f, p)(); //fine

    bind(&X::f, &x)(); //fine

    bind(&X::f, ref(x))(); //fine

    bind(&X::f, x)(); //will not compile

    return 0;
    }

    /*********************************************************************/

    I can bind the member function ponter to a smart pointer, to an
    ordinary pointer, or to a reference wrapper but I get compile errors
    in the last case.
    The documentation suggests that bind should make a copy of x. I have
    used two compilers (MSVC 2003 and gcc 3.2) and both of them barf on
    the last invocation of bind.

    The GCC error message is:

    c:/boost/boost/bind/mem_fn_template.hpp: In member function `R
    boost::_mfi::mf0<R, T>::call(U&, const T*) const [with U = const X,
    R =
    void, T = X]':
    c:/boost/boost/bind/mem_fn_template.hpp:51: instantiated from `R
    boost::_mfi::
    mf0<R, T>::eek:perator()(U&) const [with U = const X, R = void, T = X]'
    c:/boost/boost/bind.hpp:186: instantiated from `R
    boost::_bi::list1<A1>::eek:pera
    tor()(boost::_bi::type<R>, F, A&) const [with R = void, F =
    boost::_mfi::mf0<voi
    d, X>, A = boost::_bi::list0, A1 = boost::_bi::value<X>]'
    c:/boost/boost/bind/bind_template.hpp:21: instantiated from
    `boost::_bi::resul
    t_traits<R, F>::type boost::_bi::bind_t<R, F, L>::eek:perator()() [with R
    = void, F
    = boost::_mfi::mf0<void, X>, L =
    boost::_bi::list1<boost::_bi::value<X> >]'
    b.cpp:36: instantiated from here
    c:/boost/boost/bind/mem_fn_template.hpp:32: invalid conversion from
    `const X*'
    to `X*'

    Has anybody encountered this?

    Thanks.
     
    P G, Aug 8, 2004
    #1
    1. Advertising

  2. In article <>,
    (P G) wrote:

    > /**************************************************************************/
    >
    > #include <boost/function.hpp>
    > #include <boost/bind.hpp>
    > #include <boost/shared_ptr.hpp>
    > #include <iostream>
    > #include <memory>
    >
    > using namespace boost;
    >
    > struct X{
    >
    > void f()
    > {
    > std::cout<<"Bind Test\n";
    > }
    > };
    >
    > int main()
    > {
    >
    > X x;
    >
    > shared_ptr<X> p(new X());
    >
    > bind(&X::f, p)(); //fine
    >
    > bind(&X::f, &x)(); //fine
    >
    > bind(&X::f, ref(x))(); //fine
    >
    > bind(&X::f, x)(); //will not compile
    >
    > return 0;
    > }
    >
    > /*********************************************************************/
    >
    > I can bind the member function ponter to a smart pointer, to an
    > ordinary pointer, or to a reference wrapper but I get compile errors
    > in the last case.
    > The documentation suggests that bind should make a copy of x. I have
    > used two compilers (MSVC 2003 and gcc 3.2) and both of them barf on
    > the last invocation of bind.


    Your sample compiles for me using Metrowerks Pro 9 and boost 1.31.0.
    I.e. the boost authors intend for your sample to compile.

    -Howard
     
    Howard Hinnant, Aug 8, 2004
    #2
    1. Advertising

  3. P G

    P G Guest

    Howard Hinnant <> wrote in message news:<>...
    > In article <>,
    > (P G) wrote:
    >
    > > /**************************************************************************/
    > >
    > > #include <boost/function.hpp>
    > > #include <boost/bind.hpp>
    > > #include <boost/shared_ptr.hpp>
    > > #include <iostream>
    > > #include <memory>
    > >
    > > using namespace boost;
    > >
    > > struct X{
    > >
    > > void f()
    > > {
    > > std::cout<<"Bind Test\n";
    > > }
    > > };
    > >
    > > int main()
    > > {
    > >
    > > X x;
    > >
    > > shared_ptr<X> p(new X());
    > >
    > > bind(&X::f, p)(); //fine
    > >
    > > bind(&X::f, &x)(); //fine
    > >
    > > bind(&X::f, ref(x))(); //fine
    > >
    > > bind(&X::f, x)(); //will not compile
    > >
    > > return 0;
    > > }
    > >
    > > /*********************************************************************/
    > >
    > > I can bind the member function ponter to a smart pointer, to an
    > > ordinary pointer, or to a reference wrapper but I get compile errors
    > > in the last case.
    > > The documentation suggests that bind should make a copy of x. I have
    > > used two compilers (MSVC 2003 and gcc 3.2) and both of them barf on
    > > the last invocation of bind.

    >
    > Your sample compiles for me using Metrowerks Pro 9 and boost 1.31.0.
    > I.e. the boost authors intend for your sample to compile.
    >
    > -Howard


    Thanks for verifying that I wasn't misusing the library.

    I just downloaded version 1.31 (I had version 1.30 previously) and
    both compilers now accept the same code.
     
    P G, Aug 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Toby Bradshaw
    Replies:
    6
    Views:
    1,816
    Kai-Uwe Bux
    Jun 2, 2006
  2. Replies:
    0
    Views:
    614
  3. Kira Yamato
    Replies:
    0
    Views:
    470
    Kira Yamato
    Jan 28, 2008
  4. Christopher
    Replies:
    1
    Views:
    862
    Yakov Gerlovin
    Oct 5, 2011
  5. Mark
    Replies:
    1
    Views:
    469
    Jeff Flinn
    Nov 25, 2012
Loading...

Share This Page