如何解開C lambdas的錯誤名稱?
編譯後用g -4.9.3 -std = c 11程式碼
#include <iostream> #include <typeinfo> using namespace std; int main() { cout << typeid([]{}).name() << endl; }
輸出Z4mainEUlvE_作為Linux x86_64上給定lambda的被破壞的名稱.但是,c濾鏡工具無法解開它.它只輸出給它的輸入,Z4mainEUlvE_.
如何解開它?
您可以使用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'; }
輸出:
main::{lambda()#1}
根據ofollow,noindex" target="_blank">The Documentation ,該函式返回一個使用std::malloc 分配的c-style零終止字串,呼叫者需要使用std::free 釋放.此示例使用智慧指標在範圍結束時自動釋放返回的字串.
http://stackoverflow.com/questions/36534458/how-to-unmangle-mangled-names-of-c-lambdas