boost::mem_fn和std::mem_fun在MSVC6.0上的表现
Article last modified on 2002-8-7
----------------------------------------------------------------
The information in this article applies to:
- C/C++
- Microsoft Visual C++ 6.0(SP5)
----------------------------------------------------------------
boost::mem_fn的简单介绍:
boost::mem_fn是std::mem_fun系列的一个扩展。它的文档链接为:
http://www.boost.org/libs/bind/mem_fn.html。
mem_fn最为人所熟知的作用是,将一个成员函数作用在一个容器上,就像这样std::for_each(v.begin(), v.end(), boost::mem_fn(&Shape::draw))就可以让容器vector中的每一个元素都执行一遍draw方法。
第二个用法是,它可以帮助把一个函数指针模拟得像一个函数实体(function object)。比如:
template<class It, class R, class T> void for_each(It first, It last, R (T::*pmf) () ){ std::for_each(first, last, boost::mem_fn(pmf) );}
这样,你就可以这么调用:
for_each(v.begin(), v.end(), &Shape::draw);
但是仅就这些功能,是不足以让开发者舍弃std::mem_fun[_ref]。有什么理由吗?
std::mem_fun[_ref]在MSVC6.0中的编译错误:
第一个问题:std::mem_fun为什么非要成员函数有返回值呢?
试验代码:
class Stuff
{
public:
std::string m_Name;
// This function works under VC6, because it returns a value from the function.
// Note: there is nothing special about returning an int; it could be any data
// type.
int printName()
{
std::cout << m_Name << std::endl;
return 0;
}
// This function would not work under VC6, although Josuttis provides
// a similar example.
// void printName()
// {
// std::cout << m_Name << std::endl;
// }
};
void main()
{
std::vector<Stuff> v;
Stuff s1;
s1.m_Name = "Brandon";
v.push_back(s1);
std::for_each(v.begin(), v.end(), std::mem_fun_ref(&Stuff::printName));
}
编译结果:
只有把printname成员函数声明为有返回值的形式,才能编译通过;
如果声明为void printname(),则会出现下面的编译错误:
f:\program files\microsoft visual studio\vc98\include\functional(263) : error C2562: '()' : 'void' function returning a value






