为什么在函数中boost boost boost_current_function

why boost implement BOOST_CURRENT_FUNCTION in a function

本文关键字:boost function current 函数 为什么      更新时间:2023-10-16

boost在其current_function.hpp中实现宏boost_current_function.hpp,宏将提供功能的全名,以换取例外,记录等。

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

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

....
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__)将在文件范围内失败。因此必须出现在功能中。在这里引入了助手为此检查提供功能范围。

MSDN上的文档证实了这一点:

  • __ funcsig __ 定义为包含封闭函数的签名的字符串字面。宏仅在内部定义 功能。