728x90
반응형
C++의 자료형을 확인할 필요가 있을 때가 있다. 템플릿을 이용하여 특정 자료형에 따라 다른 작업을 할 때도 사용할 수 있다. 굳이 알지 않더라도 많은 작업들을 해낼 수 있지만 가끔씩 생기는 답답함에 테스트를 해서 정리를 해 두었다.
C++ STL에서 제공하는 typeid()를 사용하면 쉽게 알아낼 수 있다.
#include "stdafx.h"
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
void* v;
int i;
long l;
double d;
float f;
cout << typeid(v).name() << endl;
cout << typeid(i).name() << endl;
cout << typeid(l).name() << endl;
cout << typeid(d).name() << endl;
cout << typeid(f).name() << endl;
return 0;
}
심지어 함수의 return형도 알 수 있다. 사실 typeid 속이라도 function을 기술하면 실행되지 않을까 걱정했지만 실행되지는 않았다. func1이 실행되면 나타나야 하는 wow!! 가 콘솔 창에 표시되지 않는 것을 확인할 수 있다.
#include <iostream>
#include <typeinfo>
using namespace std;
void func1()
{
int i = 0;
cout << "wow!!" << endl;
}
int main()
{
cout << typeid(func1()).name() << endl;
return 0;
}
728x90
반응형
'Program Language > C & C++' 카테고리의 다른 글
[C++] 디버깅 모드 종료 시 메시지 (0) | 2022.07.27 |
---|---|
[C++] Dll Unloading 시 Kernel32.dll 에러 (0) | 2022.07.26 |
[C++] async로 간단하게 thread 만들기 (0) | 2022.07.08 |
[C++] GetProcAddress가 nullptr을 Return 할 때, (0) | 2022.07.01 |
[C++] template를 이용하여 Bind 사용하기 (2) | 2022.05.25 |