桜、抹茶、白、日記

名古屋市在住のC++使いのcoderの日記だったもの。

typeid

自分メモ。
ちょっと各C++コンパイラのtypeidの結果について確認する。一年前の自分はある程度理解していて、コンパイラの違いちゃんと考慮していたようだが、今の自分は忘れてたorz
コード。

#ifndef TEST_HPP_INCLUDED
#define TEST_HPP_INCLUDED

#include <iostream>
#include <typeinfo>

struct TTest {
    int    n;
};

union UTest {
    char   c[ 4 ];
    int    n;
};

class CTest {
public:
    CTest(){}
};

static void dump() {
    char   cValue       = 0;
    int    nValue       = 0;
    double dValue       = 0.0;
    TTest  stValue1     = { 0 };
    UTest  stValue2     = { 0 };
    CTest  value1;

    std::cout << typeid( cValue   ).name() << std::endl;
    std::cout << typeid( nValue   ).name() << std::endl;
    std::cout << typeid( dValue   ).name() << std::endl;
    std::cout << typeid( stValue1 ).name() << std::endl;
    std::cout << typeid( stValue2 ).name() << std::endl;
    std::cout << typeid( value1   ).name() << std::endl;
}

#endif // TEST_HPP_INCLUDED

VC++の場合

char
int
double
struct TTest
union UTest
class CTest

Borland TurboC++ 2006 Explorerの場合

char
int
double
TTest
UTest
CTest

g++ 3.4.4(Cygwin)の場合

c
i
d
5TTest
5UTest
5CTest

g++の場合は、のabi::__cxa_demangle()を使うと、TurboC++と同じ結果が得られる。但し、__cxa_demangle()はmallocされたポインタが返るので要free。何だか巷のコードはfreeしてないのが多いので注意鴨。