By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Boost::Bind strange compilation errors

P: n/a
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>::operator()(U&) const [with U = const X, R = void, T = X]'
c:/boost/boost/bind.hpp:186: instantiated from `R
boost::_bi::list1<A1>::opera
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>::operator()() [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.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <62**************************@posting.google.com >,
qu*******@hotmail.com (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
Jul 22 '05 #2

P: n/a
P G
Howard Hinnant <hi*****@metrowerks.com> wrote in message news:<hi***************************@syrcnyrdrs-03-ge0.nyroc.rr.com>...
In article <62**************************@posting.google.com >,
qu*******@hotmail.com (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.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.