gpt4 book ai didi

c++ - 为什么boost在函数中实现BOOST_CURRENT_FUNCTION

转载 作者:太空狗 更新时间:2023-10-29 20:51:58 27 4
gpt4 key购买 nike

boost 在其 current_function.hpp 中实现了一个宏 BOOST_CURRENT_FUNCTION,宏是提供函数的全名,用于异常抛出,日志记录等。

有趣的是宏是在函数内部实现的。

这样做的特殊原因是什么?

....

namespace boost
{

namespace detail
{

inline void current_function_helper()
{

#if defined( BOOST_DISABLE_CURRENT_FUNCTION )

# define BOOST_CURRENT_FUNCTION "(unknown)"

#elif ...

#elif defined(__FUNCSIG__)

# define BOOST_CURRENT_FUNCTION __FUNCSIG__

#elif ....

#else

# define BOOST_CURRENT_FUNCTION "(unknown)"

#endif

}

} // namespace detail

} // namespace boost

....

最佳答案

在查看完整实现后,您会发现它使用了几个编译器特定的“函数签名”宏。这些宏当然只在函数内有效。

测试 #elif defined(__FUNCSIG__) 将在文件范围内失败。因此必须出现在函数内部。这里引入了 helper 来为这个检查提供一个函数范围。

documentation on MSDN证实这一点:

  • __FUNCSIG__ Defined as a string literal that contains the signature of the enclosing function. The macro is defined only within a function.

关于c++ - 为什么boost在函数中实现BOOST_CURRENT_FUNCTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47647176/

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