Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why boost implement BOOST_CURRENT_FUNCTION in a function

Tags:

c++

macros

boost

boost implements a macro BOOST_CURRENT_FUNCTION in its current_function.hpp , the macro is to provide the full name of the function, for exception throw, logging etc.

What is interesting is that the macro is implemented inside a function.

What's the special reason of doing so?

....

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

....
like image 242
athos Avatar asked Dec 05 '17 06:12

athos


1 Answers

Upon looking at the full implementation one sees that it makes use of several compiler specific "function signature" macros. Those macros are of course only valid inside a function.

The test #elif defined(__FUNCSIG__) is going to fail at file scope. And so must appear inside a function. Here the helper is introduced to supply a function scope for this check.

The documentation on MSDN confirms this:

  • __FUNCSIG__ Defined as a string literal that contains the signature of the enclosing function. The macro is defined only within a function.
like image 198
StoryTeller - Unslander Monica Avatar answered Nov 15 '22 06:11

StoryTeller - Unslander Monica