C++에서 typeid 연산자 사용

Jinku Hu 2023년10월12일
C++에서 typeid 연산자 사용

이 기사에서는 C++에서typeid연산자를 사용하는 방법을 설명하고 보여줍니다.

typeid연산자를 사용하여 C++에서 개체의 유형 이름 검색

typeid연산자를 사용하여 주어진 표현식 또는 객체의 유형 정보를 검색 할 수 있습니다. <typeinfo>헤더에 정의 된std::type_info표준 라이브러리 유형에 대한 참조를 리턴합니다.

std::type_info오브젝트에는 인수 오브젝트의 기본 유형을 설명하는 문자열을 리턴하는 데 활용할 수있는 멤버 함수name이 있습니다. typeid로 검색되는 유형 정보는 구현에 따라 다릅니다. 즉, 가장 일반적인 컴파일러gccclang은 다음 코드 예제에서 설명하는 것처럼 잘린 이름을 반환합니다.

Microsoft Visual C++와 같은 다른 구현은 정보를보다 읽기 쉬운 형식으로 표시합니다. Stil, 이전 구현에서 표시 한 내장 유형의 이름을 식별하는 방법을 계속 배울 수 있습니다. 예를 들어,int개체는i문자를 반환하고int개체에 대한 포인터는Pi문자열을 반환합니다.

#include <iostream>
#include <string>
#include <typeinfo>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
  int i1 = 1234;
  string str1("arbitrary string");
  auto ptr = &i1;

  cout << "i1 type: " << typeid(i1).name() << '\n'
       << "str1 type: " << typeid(str1).name() << '\n'
       << "ptr type: " << typeid(ptr).name() << endl;

  const std::type_info& r1 = typeid(i1);
  cout << '\n' << "i1 type: " << r1.name() << '\n';

  return EXIT_SUCCESS;
}

출력:

i1 type: i
str1 type: NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
ptr type: Pi

i1 type: i

반면에 객체에 대한 참조에 대해typeid명령을 호출하면 검색된 유형 정보가 항상 정확하지 않고 때로는 읽을 수 없을 정도로 암호화되어 있음을 알 수 있습니다. 다음 예제는typeid연산자에 대한 두 번째 호출이 유형 정보를 잘못 리턴하는 경우를 보여줍니다.

#include <iostream>
#include <string>
#include <typeinfo>

using std::cout;
using std::endl;

struct Base {};
struct Derived : Base {};

struct Base2 {
  virtual void foo() {}
};
struct Derived2 : Base2 {};

int main() {
  Derived d1;
  Base& b1 = d1;
  cout << "non-polymorphic base: " << typeid(b1).name() << '\n';

  Derived2 d2;
  Base2& b2 = d2;
  cout << "polymorphic base: " << typeid(b2).name() << '\n';

  return EXIT_SUCCESS;
}

출력:

non-polymorphic base: 4Base
polymorphic base: 8Derived2

이 동작은 유형 정보를 정확하게 반환 할 수있는 Boost TypeIndex 라이브러리 함수type_id_with_cvr를 사용하여 수정할 수 있습니다. 일반적으로const,volatile,&&&수정자가있는 개체는 모두typeid연산자를 사용할 때 잘못된 유형 정보를 발생시킵니다. 따라서type_id_with_cvr함수에 더 잘 의존해야합니다.

다음 코드 예제에서는 시스템에 Boost 라이브러리를 설치해야하며type_id_with_cvr함수는boost::typeindex네임 스페이스 아래에 있습니다.

#include <boost/type_index.hpp>
#include <iostream>
#include <string>
#include <typeinfo>

using boost::typeindex::type_id_with_cvr;
using std::cout;
using std::endl;

struct Base {};
struct Derived : Base {};

struct Base2 {
  virtual void foo() {}
};
struct Derived2 : Base2 {};

int main() {
  Derived d1;
  Base& b1 = d1;

  Derived2 d2;
  Base2& b2 = d2;

  cout << type_id_with_cvr<decltype(b1)>().pretty_name() << '\n';
  cout << type_id_with_cvr<decltype(b2)>().pretty_name() << '\n';

  return EXIT_SUCCESS;
}

출력:

Base&
Base2&
작가: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

관련 문장 - C++ Operator