2015年5月25日月曜日

typeid (C++)

typeid演算子を利用して、型情報を取得できる。typeid演算子は、type_infoオブジェクト(type_infoクラスへの参照)を得る。

//
// typeinfo.cpp
// CplusplusPractice
//
// Created by masai on 2015/05/25.
// Copyright (c) 2015年 masai. All rights reserved.
//
#include <iostream>
#include <typeinfo>
#include <string>
#include <cxxabi.h>
using namespace std;
template<typename T>
void func(T t){
cout << abi::__cxa_demangle(typeid(t).name(), 0, 0, 0) << endl;
}
int main(int argc, char* argv[]){
func<int>(12);
func<double>(12.0);
func<string>("abc");
}
view raw typeid.cpp hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿