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

3 篇文章 0 订阅

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.cpp
using native typeof
Internal 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:11
BMAKE: 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 work
		boost::bind(
		std::less<double>(),
		boost::bind<double>(&PointOnProfile::getlengthAlong, _1),
		boost::bind<double>(&PointOnProfile::getlengthAlong, _2)));

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值