C++ boost::bind和成员函数返回auto_ptr时出现问题
为什么此代码无法使用VS 2005编译:C++ boost::bind和成员函数返回auto_ptr时出现问题,c++,boost-bind,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 ()
#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);
}
};
#包括
#包括
结构X
{
typedef std::auto_ptr IntType;
//typedef int IntType;//这是有效的
IntType memfunc()常量
{
返回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> ]
1>j:\libraries\boost\boost\u 1\u 37\u 0\boost\bind.hpp(1643):警告C4180:应用于函数类型的限定符没有意义;忽略
1> j:\libraries\boost\boost\u 1\u 37\u 0\boost\bind.hpp(1677):请参阅正在编译的类模板实例化“boost::\u bi::add\u cref”的参考
1> 与
1> [
1> Pm=std::auto_ptr(uu thistcall X::*)(void),
1> I=1
1> ]
1> j:\dev\test\test\listtest.cpp(16):请参阅对正在编译的类模板实例化“boost::_bi::dm_result”的引用
1> 与
1> [
1> Pm=X::IntType(uu thiscall X::*)(void),
1> A1=X*
1> ]
?
将IntType typedef更改为仅一个int允许它进行编译。我不知道,但是您是否尝试过指定返回类型的备用
bind
语法
bind<IntType>(&X::memfunc, this);
bind<std::auto_ptr<int> >(&X::memfunc, this);
bind(&X::memfunc,this);
绑定(&X::memfunc,this);
看起来,尽管文档声称它们是等效的,但以下替代方案仍然有效:
boost::bind<IntType> (boost::mem_fn (&X::memfunc), this);
boost::bind(boost::mem_fn(&X::memfunc),this);
参考图…供参考:gcc 4.3.3可以很好地编译代码。尽管如此,它仍然可以工作:boost::bind(boost::mem_fn(&X::memfunc),This);说“void f()const”是无效的。也就是说,在不使用typedef的情况下为函数类型引入常量,而不是为成员指针类型引入常量(如“void(M::*)()const”)是无效的。你必须说“typedef void f()const”;然后可以使用“f”作为const-qualified函数类型。在另一方面,警告也可能意味着这里的常量:template void f(T const&);忽略(没有字面上的“const-qualified”函数类型)。这种无知正式地只包含在c++1x工作文件中,在c++98中还没有,而且(据我所知)在c++03中也没有。在那些尚未包含常量的标准中,创建常量限定函数类型的尝试(如“const T”-而不是我之前评论中包含的类型)是格式错误的(导致警告或错误)。是的,这是第二类问题:有问题的行是这样的:typedef M const&type;M是一个函数类型。当然,我看了代码,但这并不能解释为什么返回类型是int而不是auto_ptr会起作用,或者为什么使用boost::mem_fn(应该是相同的)的替代公式会起作用。