__func__ improvement for namespaces

122 views
Skip to first unread message

Constantin-Flavius Nistor

unread,
Aug 21, 2017, 10:03:43 PM8/21/17
to ISO C++ Standard - Future Proposals
The main problem is that the current __func__ preprocessor macro is pretty much useless if using namespaces and overloaded functions:
namespace My::Defined::Namespace::Name::Is::Not
{
   
void Shown( int unused = 0 ) { std::cout << __func__ << std::endl; }
}

namespace My::Defined::Namespace::Name::Is::Still::Not
{
   
void Shown( int unused = 0 ) { std::cout << __func__ << std::endl; }
}

int main( )
{
   
My::Defined::Namespace::Name::Is::Not::Shown( );
   
My::Defined::Namespace::Name::Is::Still::Not::Shown( );
   
return 1;
}

prints just
Shown
Shown

If that would be a big project with many similar function names we wouldn't be able to tell where they come from exactly (well, without using __FILE__ and __LINE__). That preprocessor macro could be ideally used for easily adding debug messages without those downsides, so it should ideally print something similar to
void My::Defined::Namespace::Name::Is::Not::Shown( int )
void My::Defined::Namespace::Name::Is::Still::Not::Shown( int )
(maybe for the return type and argument types another similar preprocessor macro should be added)

If you won't modify this preprocessor macro (backwards compatibility ? I don't think that's the case for this one), maybe we could have another one for a version similar to the example output I wrote above ? Maybe some __func_pretty__ or anything similar.

* Don't suggest me any compiler extension, I know about them. GCC has __PRETTY_FUNCTION__ and VS has __FUNCDNAME__ and __FUNCSIG__. Using #if #else if #else #endif or any workaround to use any existing extension of the used compiler can be unpleasant if wanting to do some simple debug lines to work on multiple compilers.

Daemon Snake

unread,
Aug 22, 2017, 8:47:12 PM8/22/17
to ISO C++ Standard - Future Proposals
Le mardi 22 août 2017 04:03:43 UTC+2, Constantin-Flavius Nistor a écrit :
The main problem is that the current __func__ preprocessor macro is pretty much useless if using namespaces and overloaded functions:
1. __func__ is not a macro.
    "C99 introduced __func__, ... Both of these are strings containing the name of the current function ...
     Neither of them is a macro; the preprocessor does not know the name of the current function."

2. __func__ is there for C compatibility,
      the C standard defines it as function name only and that's all. It's not meant to be useful in C++.

3. Reflection
      The reflection group is working on having a full static reflection which renders your proposal useless. (meta classes, reflexpr, etc...).
      For instance : https://godbolt.org/g/HBHmEC.
      You could ask for $__func__ though.
Reply all
Reply to author
Forward
0 new messages