gpt4 book ai didi

c++ - 绑定(bind)抛出异常的函数时,boost::bind 在 VC++ 2010 中不起作用

转载 作者:行者123 更新时间:2023-11-30 04:25:47 25 4
gpt4 key购买 nike

我有一些代码可以在 Linux 下正常编译,但我正在尝试将其移植到 Windows。我使用了来自 Boost Pro 的 Boost 1.50 预编译二进制文件,但是当我编译我的代码时,我得到了这个神秘的错误:

error C2664: 'boost::_bi::bind_t<R,F,L>::bind_t(const boost::_bi::bind_t<R,F,L> &)' :
cannot convert parameter 1 from 'boost::_bi::bind_t<R,F,L>'
to 'const boost::_bi::bind_t<R,F,L> &'
C:\Program Files (x86)\boost\boost_1_50\boost\bind\bind_cc.hpp [line] 50

这个错误是最无用的,因为它出现在 Boost 头文件的深处,没有指示问题出在我的代码中的哪个位置。尽管如此,通过注释掉各种代码块,我已经将其缩小为原因:

void test(int a)
throw (int) // removing this line makes it compile
{
return;
}
...
boost::function<void(int)> fn = boost::bind<void>(test, _1);

如果我在函数定义中删除 throw 说明符,它就会起作用。不管我抛出什么,无论是一个类还是一个 int。我是不是做错了什么,或者您不能绑定(bind)到在 Visual C++ 中引发异常的函数? Boost Bind 文档似乎没有提出任何问题,GCC 也没有任何问题。

[旁注:上面的代码不是我的实际代码,但编译后它会出现同样的问题。请避免评论 throwing ints 是坏的等等,因为这只是一个微不足道的例子,以防有人希望重现问题。]

最佳答案

我不知道为什么你的代码在 VC++ 上失败。但是,通常最好避免异常规范,因为它们会带来非常微妙的影响。看这个优秀专栏A Pragmatic Look at Exception Specifications通过 Herb Sutter:

So here’s what seems to be the best advice we as a community have learned as of today:

Moral #1: Never write an exception specification.

Moral #2: Except possibly an empty one, but if I were you I’d avoid even that.

关于c++ - 绑定(bind)抛出异常的函数时,boost::bind 在 VC++ 2010 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com