Идентификация разных типов. При сравнении одного типа с другим выдаётся другое число. Типы имеют иногда свой синоним с помощью typedef
, или define
. Иногда сравнение по размеру не помогает. Это аналог системной функции typeid
.
// $ g++ -Wall tempstat.cpp
# include <iostream>
using ComponentID = std::size_t;
inline ComponentID getComponentTypeID() {
static ComponentID lastID = 0;
return lastID++;
}
template<typename T> inline ComponentID getComponentTypeID() noexcept {
static ComponentID typeID = getComponentTypeID();
return typeID;
}
typedef unsigned char bytec ;
typedef uint8_t byte8 ;
int main(){
std::cout<<"int = "<<getComponentTypeID<int>()<<std::endl;
std::cout<<"char = "<<getComponentTypeID<char>()<<std::endl;
std::cout<<"unsigned char = "<<getComponentTypeID<unsigned char>()<<std::endl;
std::cout<<"bytec = "<<getComponentTypeID<bytec>()<<std::endl;
std::cout<<"byte8 = "<<getComponentTypeID<byte8>()<<std::endl;
std::cout<<"int = "<<typeid(int).name()<<std::endl;
std::cout<<"char = "<<typeid(char).name()<<std::endl;
std::cout<<"unsigned char = "<<typeid(unsigned char).name()<<std::endl;
std::cout<<"bytec = "<<typeid(bytec).name()<<std::endl;
std::cout<<"byte8 = "<<typeid(byte8).name()<<std::endl;
}
Результат:
int = 0
char = 1
unsigned char = 2
bytec = 2
byte8 = 2
int = i
char = c
unsigned char = h
bytec = h
byte8 = h
Результат показывает, что типы char
и unsigned char
- разные. А uint8_t
- это синоним типа unsigned char
.
getComponentTypeID
(инициализирующий переменнуюtypeID
только один раз) будет возвращать новый результат