Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Boost::Bind strange compilation errors

Reply
Thread Tools

Boost::Bind strange compilation errors

 
 
P G
Guest
Posts: n/a
 
      08-08-2004
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>:perator()(U&) const [with U = const X, R = void, T = X]'
c:/boost/boost/bind.hpp:186: instantiated from `R
boost::_bi::list1<A1>: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>: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.
 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      08-08-2004
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
P G
Guest
Posts: n/a
 
      08-09-2004
Howard Hinnant <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> In article <(E-Mail Removed) >,
> (E-Mail Removed) (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compilation error with seperate compilation C__chp C++ 4 02-15-2008 03:57 PM
Strange compilation errors =?Utf-8?B?QXJnaXJvcA==?= ASP .Net 3 09-27-2006 05:07 PM
Newbie needs help with perl compilation errors Alfred B. Stansbury Perl 1 10-22-2004 10:55 AM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM
compilation errors Eqbal VHDL 0 11-18-2003 08:07 PM



Advertisments