7

I work with g++ 4.8.1 and use these two macros for debugging. However, the __func__ macro gives me only the function name, which might be misleading in the case you have many functions with the same name in different classes. The __PRETTY_FUNCTION__ macro produces the whole function signature - with return type, class name and all arguments, which can be very long.

I'd like to have something between - a macro, which will give me only class name and function name. Any way to achieve that?

5
  • Assuming you are in a non-static member function, you could use the type of this, typeid and gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html to get the class name. Apr 22, 2014 at 20:57
  • 2
    The class name and function name won't always uniquely identify the function. Apr 22, 2014 at 20:57
  • 1
    @KeithThompson - I can live with that, I don't need 100% precision - it's only debugging
    – HEKTO
    Apr 22, 2014 at 21:01
  • 1
    I find __FILE__ "(" STRINGIZE(__LINE__) "): " __func__ " - " works excellent, especially in MSVC which recognizes this syntax especially. Apr 22, 2014 at 21:28
  • 1
    By the way, __func__ is not a macro, that's why it's lowercase. Apr 24, 2015 at 21:28

2 Answers 2

9

Inspired by this, I created the following macro __COMPACT_PRETTY_FUNCTION__:

std::string computeMethodName(const std::string& function, const std::string& prettyFunction);

#define __COMPACT_PRETTY_FUNCTION__ computeMethodName(__FUNCTION__,__PRETTY_FUNCTION__).c_str() //c_str() is optional


std::string computeMethodName(const std::string& function, const std::string& prettyFunction) {
    size_t locFunName = prettyFunction.find(function); //If the input is a constructor, it gets the beginning of the class name, not of the method. That's why later on we have to search for the first parenthesys
    size_t begin = prettyFunction.rfind(" ",locFunName) + 1;
    size_t end = prettyFunction.find("(",locFunName + function.length()); //Adding function.length() make this faster and also allows to handle operator parenthesys!
    if (prettyFunction[end + 1] == ')')
        return (prettyFunction.substr(begin,end - begin) + "()");
    else
        return (prettyFunction.substr(begin,end - begin) + "(...)");
}

What it does:

  • It takes __PRETTY_FUNCTION__
  • It removes return type and all arguments
  • If the function has zero arguments, it appends (), otherwise (...)

Features:

  • Handles namespaces, constructors and so on
  • Works also with the parenthesis operator!

Limitations:

  • It only works with gcc
  • Created at runtime rather than compile time
  • Heap allocated.
  • Does not work for lambdas, __FUNCTION__ and __PRETTY_FUNCTION__ don't match... I would almost call it a compiler bug :)
    • __FUNCTION__ sees an operator()
    • __PRETTY_FUNCTION__ sees <lambda(...)>
10
  • 2
    Limitations: Created at runtime rather than compile time, and is heap allocated. Apr 24, 2015 at 20:19
  • @MooingDuck Is there a way to avoid this, for example with constexpr in C++11? Can you help me with that? (In my case it is not so important because used only during debugging).
    – Antonio
    Apr 24, 2015 at 20:20
  • Well, if you're crafty, you could potentially add something super complicated to place the names into a compile-time buffer, but it'd be hard, I probably wouldn't bother. However, I would alter computeMethodName to use const char(&)[N] as much as possible to reduce the number of heap allocations. I'll whip something up. Apr 24, 2015 at 20:47
  • 1
    Yours actually seems to work for pretty complex cases too! coliru.stacked-crooked.com/a/8d2dd99099c813f0 But not pathalogical cases: coliru.stacked-crooked.com/a/6a09d229735cfc72 Apr 24, 2015 at 21:03
  • 1
    Made a version with zero allocations, though the method name is only valid until the next call in the same thread. I figure that's livable. coliru.stacked-crooked.com/a/132eb50d00b4cb7f Doesn't quite get pathalogical cases right, but pretty good. Apr 24, 2015 at 21:30
3

Unfortunatly, I don't think this can be done easily. I am one of those that don't understand why nobody ever proposed the implementation of a __CLASS__ macro, that could expand to the current class, similarly to all the macros defined by GCC, for example.

I agree that these macros are great help in some difficult debugging situations. Probably difficult to implement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.