chatgpt의 도움을 받은 글입니다.
강의를 듣다가 typeid 라는걸 처음봐서 기록한다.
#define WRITE_LOCK_IDX(idx) WriteLockGuard writeLockGuard_##idx(_locks[idx], typeid(this).name());
위 #define은 WRITE_LOCK_IDX(idx) 을 WriteLockGuard writeLockGuard_##idx(_locks[idx], typeid(this).name()); 대신해 쓴다는 것일 뿐이니 이 글에서 (idx)나 ##idx 를 다룬 부분은 다른 글을 참조하자.
typeid 는 c++ 키워드다
처음엔 함수이름인가? 했는데 if 나 class와 같은 예약어 중 하나인 키워드라고 한다.
C는 정적 타입 언어이기 때문에 런타임에 타입 정보 추적이 불가능했다고 한다. 그래서 c++에서 런타임에 타입 정보를 가져올 수 있는 typeid 라는 키워드가 추가되었다.
#include <iostream>
#include <typeinfo>
int main() {
int x = 42;
std::cout << typeid(x).name() << std::endl; // "int" 출력됨
}
class Client {
public:
void announce() {
std::cout << typeid(this).name() << std::endl;
}
};
int main()
{
Client client_01;
client_01.announce(); // "class Client * __ptr64" 출력됨
}
정확히는 typeid()는 const type_info& 을 리턴하고 type_info 객체에 대한 참조자라고 한다. name()은 멤버함수 중 하나인듯 하다.
'공부 > C\C++' 카테고리의 다른 글
| c++ using typedef 차이 (0) | 2025.06.28 |
|---|---|
| c 매크로 - ## (토큰 결합 연산자) (0) | 2025.06.28 |
| c extern 키워드 (with 선언과 정의) (0) | 2025.06.24 |
| std::atomic<T>::compare_exchange_weak (0) | 2025.06.14 |
| std::move (0) | 2025.06.11 |