반응형
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
반응형
'프로그래밍 언어 > C++' 카테고리의 다른 글
C++ 템플릿 타입 추론(배열의 주소) (0) | 2019.07.14 |
---|---|
C++ 템플릿 타입 추론(Template Argument Type Deduction) (0) | 2019.07.14 |
C++ 비주얼스튜디오에서 boost 라이브러리 사용 방법 (0) | 2019.06.30 |
C++ 지연 인스턴스화(Lazy Instantiation) (0) | 2019.05.28 |
C++ 템플릿 주의사항/template instantiation (0) | 2019.05.28 |