Boost::Bind strange compilation errors

P

P G

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

Howard Hinnant

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

#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
 
P

P G

Howard Hinnant said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top