この質問/回答が注目され、GManNickGから貴重なフィードバックが寄せられたため、コードを少し整理しました。2 つのバージョンが提供されています。1 つは C++11 機能を備えたもので、もう 1 つは C++98 機能のみを備えたものです。
ファイルtype.hpp 内
#ifndef TYPE_HPP
#define TYPE_HPP
#include <string>
#include <typeinfo>
std::string demangle(const char* name);
template <class T>
std::string type(const T& t) {
return demangle(typeid(t).name());
}
#endif
ファイルtype.cpp 内(C++11 が必要)
#include "type.hpp"
#ifdef __GNUG__
#include <cstdlib>
#include <memory>
#include <cxxabi.h>
std::string demangle(const char* name) {
int status = -4; // some arbitrary value to eliminate the compiler warning
// enable c++11 by passing the flag -std=c++11 to g++
std::unique_ptr<char, void(*)(void*)> res {
abi::__cxa_demangle(name, NULL, NULL, &status),
std::free
};
return (status==0) ? res.get() : name ;
}
#else
// does nothing if not g++
std::string demangle(const char* name) {
return name;
}
#endif
使用法:
#include <iostream>
#include "type.hpp"
struct Base { virtual ~Base() {} };
struct Derived : public Base { };
int main() {
Base* ptr_base = new Derived(); // Please use smart pointers in YOUR code!
std::cout << "Type of ptr_base: " << type(ptr_base) << std::endl;
std::cout << "Type of pointee: " << type(*ptr_base) << std::endl;
delete ptr_base;
}
それは印刷します:
ptr_base のBase*
タイプ: 指示先のタイプ:Derived
g++ 4.7.2、g++ 4.9.0 20140302 (実験的)、clang++ 3.4 (トランク 184647)、clang 3.5 (トランク 202594)、Linux 64 ビットおよび g++ 4.7.2 (Mingw32、Win32 XP SP2) でテスト済み。
C++11 の機能を使用できない場合、C++98 で行う方法は次のとおりです。ファイルtype.cppは次のようになります。
#include "type.hpp"
#ifdef __GNUG__
#include <cstdlib>
#include <memory>
#include <cxxabi.h>
struct handle {
char* p;
handle(char* ptr) : p(ptr) { }
~handle() { std::free(p); }
};
std::string demangle(const char* name) {
int status = -4; // some arbitrary value to eliminate the compiler warning
handle result( abi::__cxa_demangle(name, NULL, NULL, &status) );
return (status==0) ? result.p : name ;
}
#else
// does nothing if not g++
std::string demangle(const char* name) {
return name;
}
#endif
(2013年9月8日更新)
受け入れられた回答 (2013 年 9 月 7 日現在)の呼び出しabi::__cxa_demangle()
が成功すると、スタックに割り当てられたローカル配列へのポインターが返されます。
また、バッファを提供する場合はabi::__cxa_demangle()
、ヒープに割り当てられると想定することに注意してください。スタックにバッファを割り当てるのはバグです (gnu doc から): "If output_buffer
is not long, it is expand using realloc
." スタックへのポインタを呼び出しrealloc()
ています... ああ!( Igor Skochinskyの親切なコメントも参照してください。)
これらのバグの両方を簡単に確認できます。受け入れられた回答 (2013 年 9 月 7 日現在) のバッファー サイズを 1024 から 16 などの小さいサイズに減らし、 15 を超えない名前の何かを付けますrealloc()
(呼び出されません)。それでも、システムとコンパイラの最適化に応じて、出力は次のようになります: ガベージ / 何もない / プログラム クラッシュ。
2 番目のバグを確認するには、バッファ サイズを 1 に設定し、名前が 1 文字より長いもので呼び出します。実行するとrealloc()
、スタックへのポインターを使用して呼び出しを試みるため、プログラムはほぼ確実にクラッシュします。
(2010 年 12 月 27 日の古い回答)
KeithB のコードに加えられた重要な変更:バッファーは、malloc によって割り当てられるか、NULL として指定される必要があります。スタックに割り当てないでください。
その状態もチェックしておいた方が賢明です。
見つかりませんでしたHAVE_CXA_DEMANGLE
。__GNUG__
コードがコンパイルされることを保証するものではありませんが、チェックします。誰でも良いアイデアがありますか?
#include <cxxabi.h>
const string demangle(const char* name) {
int status = -4;
char* res = abi::__cxa_demangle(name, NULL, NULL, &status);
const char* const demangled_name = (status==0)?res:name;
string ret_val(demangled_name);
free(res);
return ret_val;
}