1

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.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
jon hanson
  • 8,722
  • 2
  • 37
  • 61

3 Answers3

3

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
  • 8,722
  • 2
  • 37
  • 61
  • 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 May 12 '09 at 10:05
  • On the other side, the warning could also mean that the const here: template 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 May 12 '09 at 10:09
  • yes, it's the second kind of issue: the line in question does this: typedef M const & type; with M being a function type. thus trying to literally const-qualifying a function-type, yielding to the correct warning. – Johannes Schaub - litb May 12 '09 at 10:12
  • 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 May 12 '09 at 10:34
  • Or why the alternative formulation using boost::mem_fn (which is supposed to be the same) works. – jon hanson May 12 '09 at 10:36
0

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
  • 131,367
  • 29
  • 160
  • 239
0

FYI: gcc 4.3.3 compiles the code fine.

James Hopkin
  • 13,797
  • 1
  • 42
  • 71