__PRETTY_FUNCTION__:返回类型可以包含括号吗?

__PRETTY_FUNCTION__: Can a return type contain parentheses?

本文关键字:包含括 PRETTY FUNCTION 返回类型      更新时间:2023-10-16

我正在考虑这个解决方案来减少__PRETTY_FUNCTION__的输出。该解决方案删除了返回类型、参数和修饰符。

我想知道以下修改是否在任何情况下都有效:

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;
    (...)
}

也就是说,我想了解返回类型(或其他任何内容,在函数名称左侧由 __PRETTY_FUNCTION__ 返回的字符串中)是否可能包含一个左括号(


我以不同的方式实现了该方法。

是的,可以有其他括号。 下面是一个示例:

#include <iostream>
using fptr = void(*)();
fptr func() {
    std::cout << __PRETTY_FUNCTION__ << 'n';
    return nullptr;
}
int main()
{
    func();
}

使用 g++ -std=c++14 的输出为:

void (* func())()