Antonio
Antonio

Reputation: 20336

__PRETTY_FUNCTION__: Can a return type contain parentheses?

I was considering this solution to reduce the output of __PRETTY_FUNCTION__. That solution removes return type, arguments and modifiers.

I was wondering if the following modification would work in any case:

inline std::string methodName(const std::string& prettyFunction) {
    size_t parenthesis = prettyFunction.find("("); //Then I can use parenthesis index as end for my string
    size_t begin = prettyFunction.rfind(" ",parenthesis) + 1;
    (...)
}

Namely, I would like to understand if there's any chance that the return type (or anything else, in the string returned by __PRETTY_FUNCTION__, at the left of the function name) contains an open parentheses (


I implemented the method in a different way.

Upvotes: 0

Views: 222

Answers (1)

aschepler
aschepler

Reputation: 72483

Yes, there can be other parentheses. Here's an example:

#include <iostream>

using fptr = void(*)();

fptr func() {
    std::cout << __PRETTY_FUNCTION__ << '\n';
    return nullptr;
}

int main()
{
    func();
}

Output using g++ -std=c++14 is:

void (* func())()

Upvotes: 2

Related Questions