gpt4 book ai didi

c++ - 如果在 user.hpp 中定义了 BOOST_NO_EXCEPTIONS,为什么不能编译 boost::shared_ptr

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:22:36 31 4
gpt4 key购买 nike

我有一个嵌入式系统,想在这个系统中使用 boost,但需要禁用异常,因为我不想支付异常成本。

boost给了一个user.hpp和可设置的宏选项BOOST_NO_EXCEPTIONSBOOST_NO_EXCEPTION_STD_NAMESPACE,但是boost::shared_ptr不能编译(更准确的说,不能链接) 如果定义了这两个宏。

shared_ptr_boost.cpp:(.text._ZN5boost6detail12shared_countC2IiEEPT_[_ZN5boost6detail12shared_countC5IiEEPT_]+0x7a): undefined reference to `boost::throw_exception(std::exception const&)'
collect2: error: ld returned 1 exit status

为什么 boost 提供宏选项但不 promise 使用这些选项进行编译?

最佳答案

可以编译。

只是无法链接。

那是因为如果您定义 BOOST_NO_EXCEPTIONS,则必须在某处提供 boost::throw_exception(std::exception const&) 的实现,以替换通常的错误引发工具。

阅读 throw_exception.hpp 中的注释:

namespace boost
{

#ifdef BOOST_NO_EXCEPTIONS

void throw_exception(std::exception const & e); // user defined

#else

//[Not user defined --Dynguss]
template<class E> inline void throw_exception(E const & e)
{
throw e;
}

#endif

} // namespace boost

关于c++ - 如果在 user.hpp 中定义了 BOOST_NO_EXCEPTIONS,为什么不能编译 boost::shared_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33664088/

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