DataType 출력하기
21 Jan 2019 | C++C++ DataType 출력하기
typeid 명령어 이용
리눅스에서 gcc 기반으로 실행했을 때 Windows에서 Visual Studio로 실행했을 때의 결과가 조금 다릅니다.
void test01() {
int a = 10;
double b = 10.0;
string c = "abc";
bool d = true;
cout << typeid(a).name() << ", " << typeid(b).name() << ", " << typeid(c).name() << ", " << typeid(d).name()
<< endl;
}
i, d, NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, b
std::is_same 명령어 이용
template<typename T>
std::string getType(T) {
std::string type = "unknown";
if (std::is_same<T, int>::value) type = "int";
if (std::is_same<T, double>::value) type = "double";
if (std::is_same<T, float>::value) type = "float";
if (std::is_same<T, bool>::value) type = "bool";
if (std::is_same<T, string>::value) type = "string";
return type;
}
void test02() {
int a = 10;
double b = 10.0;
string c = "abc";
bool d = true;
cout << getType(a) << ", " << getType(b) << ", " << getType(c) << ", " << getType(d) << endl;
}
int, double, string, bool