반응형

std::typeid 활용

  • C++ 표준의 typeid() 연산자 사용
    • const, volatile, reference 구분하여 조사할 수 없음
#include <iostream>
using namespace std;

template<typename T> void foo(const T a)
{
    cout << "T : " << typeid(T).name() << endl;
    cout << "a : " << typeid(a).name() << endl;
}

int main()
{
    foo(3);
    foo(3.3);
}

결과

int
int
double
double

 

boost:type_index 활용

  • boost::type_id_with_scv<T>().pretty_name() 사용
  • const, volatile, reference 구분하여 조사할 수 있음
  • <boost/type_index.hpp>
  • namespace boost::typeindex 안에 포함
  • 변수의 타입을 조사 할때는 decltype()을 이용
    • type_id_with_cvr<decltype(a)>().pretty_name()
  • boost 사용법 참고 : https://www.devoops.kr/77
#include <iostream>
#include <boost/type_index.hpp>
using namespace std;
using namespace boost::typeindex;

template<typename T> void foo(const T a) // 실제 템플릿 타입 T와 변수타입은 다를 수 있음
{
    //cout << "T : " << typeid(T).name() << endl; // int, double
    //cout << "a : " << typeid(a).name() << endl; // int, double : std의 typeid로는 구분 불가
    cout << type_id_with_cvr<T>().pretty_name() << endl;
    cout << type_id_with_cvr<decltype(a)>().pretty_name() << endl;
}

int main()
{
    foo(3);
    foo(3.3);
}

결과

int
int const
double
double const

 

 

 

반응형

+ Recent posts