boost的bind为何导致vc2005编译器崩溃

来源:互联网 发布:古希腊人口知乎 编辑:程序博客网 时间:2024/06/05 20:02

bind与标准库配合可以少些不少的FunctionObject。代码极易维护。

但似乎某些编译器支持不好,即使是VC2005.

下面这段代码是不能通过编译的:

std::sort(profPointsP.begin(),profPointsP.end(),boost::bind(std::less<double>(),boost::bind(&PointOnProfile::getlengthAlong, _1),boost::bind(&PointOnProfile::getlengthAlong, _2)));

导致编译器错误:

[== Building d:\PROGRA~1\Bentley\MICROS~1\MICROS~1\mdl\objects\createAlignment.obj, (J:\MDL_Study\vs_ma_ide\IDE\createAlignment.cpp) ==]createAlignment.cppusing native typeofInternal Compiler Error in D:\Program Files (x86)\Microsoft Visual Studio 8\VC\BIN\cl.exe.  You will be prompted to send an error report to Microsoft later.Sun Jul 22 13:30:26 2012, elapsed time: 0:11BMAKE: file [d:\PROGRA~1\Bentley\MICROS~1\MICROS~1\mdl\objects\createAlignment.obj] may be incomplete

 

应该说,下面这段代码与上面的是等价的,但可以通过编译:

 

std::sort(profPointsP.begin(),profPointsP.end(),boost::bind(std::less<double>(), boost::bind<double>(mem_fn(&PointOnProfile::getlengthAlong), _1), boost::bind<double>(mem_fn(&PointOnProfile::getlengthAlong), _2)));


遍查boost资料,不得奥妙。

《boost库导论》有云:“对于有缺陷的编译器无法推断出返回类型的上下文,如果函数对象中包含了result_type,那么就不再需要显式地声明返回类型”。

将代码修改如下就可以了:

std::sort(profPointsP.begin(),profPointsP.end(),   //can't workboost::bind(std::less<double>(),boost::bind<double>(&PointOnProfile::getlengthAlong, _1),boost::bind<double>(&PointOnProfile::getlengthAlong, _2)));

 原来 VC2005的编译器是有缺陷的呀:)