g++abi::__cxa_demangle
函数不返回成员函数的返回值是什么原因?
这是此行为的一个工作示例
#include <execinfo.h>
#include <cxxabi.h>
#include <iostream>
struct Foo {
void operator()() const
{
constexpr int buf_size = 100;
static void *buffer[buf_size];
int nptrs = backtrace(buffer, buf_size);
char **strings = backtrace_symbols(buffer, nptrs);
for(int i = 0; i < nptrs; ++i) {
auto str = std::string(strings[i]);
auto first = str.find_last_of('(') + 1;
auto last = str.find_last_of(')');
auto mas = str.find_last_of('+');
int status;
char* result = abi::__cxa_demangle(str.substr(first, mas-first).c_str(), nullptr, nullptr, &status);
if (status == 0) std::cout << result << std::endl;
}
}
};
int main () {
Foo f;
f();
}
编译后
g++ foo.cpp -std=c++11 -rdynamic
输出是
Foo::operator()() 常量
有没有办法获得一致的行为,也就是获取成员函数的返回值?
最佳答案
函数名称的修饰/反修饰超出了 C++ 标准的范围。以两种不同的函数将具有不同的修饰表示(符号)的方式来修饰函数纯粹是编译器的责任和必要性。
同时,由于标准禁止声明仅返回类型不同的两个函数,因此编译器无需破坏返回类型,因为这没有任何好处。
您所看到的只是编译器相当懒惰的结果。
此外,这是一个 rextester有一个更完整的示例(包括回溯中的全局函数)。
关于c++ - 分解后的成员函数名称中的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44914938/