tags:

views:

418

answers:

3

Why does this code fail to compile with VS 2005:

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

struct X
{
    typedef std::auto_ptr<int> IntType;
    // typedef int IntType; // this works

    IntType memfunc () const
    {
        return IntType ();
    }

    X ()
    {
        boost::bind (&X::memfunc, this);
    }
};

with this warning & error:

1>j:\libraries\boost\boost_1_37_0\boost\bind.hpp(1643) : warning C4180: qualifier applied to function type has no meaning; ignored
1>        j:\libraries\boost\boost_1_37_0\boost\bind.hpp(1677) : see reference to class template instantiation 'boost::_bi::add_cref<Pm,I>' being compiled
1>        with
1>        [
1>            Pm=std::auto_ptr<int> (__thiscall X::* )(void),
1>            I=1
1>        ]
1>        j:\dev\test\test\listtest.cpp(16) : see reference to class template instantiation 'boost::_bi::dm_result<Pm,A1>' being compiled
1>        with
1>        [
1>            Pm=X::IntType (__thiscall X::* )(void),
1>            A1=X *
1>        ]

?

Changing the IntType typedef to just an int allows it to compile.

A: 

I don't know, but have you tried the alternate bind syntax where you specify the return type?

bind<IntType>(&X::memfunc, this);
bind<std::auto_ptr<int> >(&X::memfunc, this);
1800 INFORMATION
It gives the same error.
jon hanson
This works though: boost::bind<IntType> (boost::mem_fn (
jon hanson
+3  A: 

It seems, that, despite the documentation claiming they are equivalent, the following alternative works:

boost::bind<IntType> (boost::mem_fn (&X::memfunc), this);

Go figure...

jon hanson
It's not valid to say "void f() const". That is, introducing a const for a function type without using typedef and when it's not for a member-pointer type (like "void(M::*)() const") is invalid. You have to say "typedef void f() const"; then you can use "f" as a function-type const-qualified. maybe boost does otherwise somewhere in its code.
Johannes Schaub - litb
On the other side, the warning could also mean that the const here: template<typename T> void f(T const is ignored (there are no literally "const qualified" function-types). That ignorance is formally only contained in the c++1x working paper, and not yet in c++98 and not (to my knowledge) in c++03. In those standards where it's not yet contained, such a try to create a const-qualified function typoe (as in "const T" - not as in the one contained in my previous comment) is ill-formed (results in a warning or error).
Johannes Schaub - litb
yes, it's the second kind of issue: the line in question does this: typedef M const with M being a function type. thus trying to literally const-qualifying a function-type, yielding to the correct warning.
Johannes Schaub - litb
Sure,i looked at the code, but that doesn't explain why it works if the return type is an int instead of an auto_ptr?
jon hanson
Or why the alternative formulation using boost::mem_fn (which is supposed to be the same) works.
jon hanson
A: 

FYI: gcc 4.3.3 compiles the code fine.

James Hopkin