jot*_*tik 16 c++ lambda name-mangling c++11
用g++-4.9.3 -std=c++11
代码编译完成后
#include <iostream>
#include <typeinfo>
using namespace std;
int main() { cout << typeid([]{}).name() << endl; }
Run Code Online (Sandbox Code Playgroud)
输出Z4mainEUlvE_
作为Linux x86_64上给定lambda的受损名称.但是,该c++filt
工具无法解开它.它只输出给它的输入,Z4mainEUlvE_
.
我怎么解开呢?
Gal*_*lik 16
您可以使用GCC的特殊abi::__cxa_demangle
功能:
#include <memory>
#include <cstdlib>
#include <cxxabi.h>
#include <iostream>
// delete malloc'd memory
struct malloc_deleter
{
void operator()(void* p) const { std::free(p); }
};
// custom smart pointer for c-style strings allocated with std::malloc
using cstring_uptr = std::unique_ptr<char, malloc_deleter>;
int main()
{
// special function to de-mangle names
int error;
cstring_uptr name(abi::__cxa_demangle(typeid([]{}).name(), 0, 0, &error));
if(!error)
std::cout << name.get() << '\n';
else if(error == -1)
std::cerr << "memory allocation failed" << '\n';
else if(error == -2)
std::cerr << "not a valid mangled name" << '\n';
else if(error == -3)
std::cerr << "bad argument" << '\n';
}
Run Code Online (Sandbox Code Playgroud)
输出:
main::{lambda()#1}
Run Code Online (Sandbox Code Playgroud)
根据The Documentation,这个函数返回一个使用std :: malloc分配的c风格的零终止字符串,调用者需要使用std :: free释放它.此示例使用智能指针在范围的末尾自动释放返回的字符串.
小智 9
使用c++filt
版本070207 20070207
:
$ c++filt -n Z4mainEUlvE_
main::'lambda'()
Run Code Online (Sandbox Code Playgroud)
虽然评论者提出这些名称并不总是完全有用.
归档时间: |
|
查看次数: |
1240 次 |
最近记录: |