代码之家  ›  专栏  ›  技术社区  ›  jon hanson

boost::bind和成员函数返回auto_ptr时出现问题

  •  1
  • jon hanson  · 技术社区  · 16 年前

    #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);
        }
    };
    

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

    3 回复  |  直到 16 年前
        1
  •  3
  •   jon hanson    16 年前

    似乎,尽管文件声称它们是等效的,但以下替代方案是可行的:

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

        2
  •  0
  •   1800 INFORMATION    16 年前

    我不知道,但你试过替代品吗 bind 指定返回类型的语法?

    bind<IntType>(&X::memfunc, this);
    bind<std::auto_ptr<int> >(&X::memfunc, this);
    
        3
  •  0
  •   James Hopkin    16 年前

    仅供参考:gcc 4.3.3可以很好地编译代码。