boost :: bind和成员函数返回auto_ptr的问题

时间:2009-05-12 08:28:22

标签: c++ boost-bind

为什么这段代码无法使用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);
    }
};

带有此警告&amp;错误:

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>        ]

将IntType typedef更改为只允许它进行编译。

3 个答案:

答案 0 :(得分:3)

看起来,尽管文档声称它们是等效的,但以下替代方案仍有效:

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

去图......

答案 1 :(得分:0)

我不知道,但您是否尝试过指定返回类型的备用bind语法?

bind<IntType>(&X::memfunc, this);
bind<std::auto_ptr<int> >(&X::memfunc, this);

答案 2 :(得分:0)

仅供参考:gcc 4.3.3编译好代码。

相关问题