I want to create different types that are uint32_t but are different from compilers perspective -- they can only be compared and assigned to a value of the exact same type. Here is a sample code that I want to achieve:
TextureResourceId t1 = 1000, t2 = 2000;
PipelineResourceId p1 = 1000, p2 = 2000;
BufferResourceId b1 = 1000, b2 = 1000;
if (t1 == t2) // OK!
if (t1 == p1) // Compiler error!
if (t1 == b1) // Compiler error!
I know that I can do some preprocessor magic to achieve this:
#define CREATE_RESOURCE_ID(NAME) \
class NAME { \
public: \
NAME(uint32_t value_): value(value_) {} \
bool operator==(const NAME &rhs) { return value == rhs.value; } \
private: \
uint32_t value; \
};
CREATE_RESOURCE_ID(TextureResourceId);
CREATE_RESOURCE_ID(BufferResourceId);
CREATE_RESOURCE_ID(PipelineResourceId);
int main() {
TextureResourceId tex1(1000), tex2(2000);
BufferResourceId buf1(1000);
PipelineResourceId pip1(1000);
if (tex1 == tex2) {}
if (buf1 == tex1) {}
if (tex1 == pip1) {}
return 0;
}
But I would like to know if there is a more C++'y way of doing this (e.g with inheritance or some kind of syntax with enum classes)