R
redqil
The problem I am facing is that an integer is the ideal representation
for many different classes I am using. I want to write overloaded
functions that are able to differentiate between the classes without
mutual interference. The naive, yet wrong, solution is:
typedef int A;
typedef int B;
void f(A a);
void f(B b); // Error
since typedef does not introduce a new type. In addition, I want the
naked expression "b = a" to error out during compile time.
The solution that I have implemented looks like this:
#define DECLARE_UNIQUE_CLASS_ALIAS(_ALIAS, _BASE)\
struct _ALIAS\
{\
_BASE value;\
_ALIAS(const _BASE& v)\
{\
this->value = v;\
}\
operator _BASE() const\
{\
return this->value;\
}\
};
DECLARE_UNIQUE_CLASS_ALIAS(A, int)
DECLARE_UNIQUE_CLASS_ALIAS(B, int)
This has the desired effect, but I am not happy with it because:
1. It uses macros, and is not debuggable.
2. Since the machine code for each alias is identical, and this will
be used a lot, this code will not be optimized by the compiler.
Could someone could suggest a solution that does not have the defects
that my code has?
for many different classes I am using. I want to write overloaded
functions that are able to differentiate between the classes without
mutual interference. The naive, yet wrong, solution is:
typedef int A;
typedef int B;
void f(A a);
void f(B b); // Error
since typedef does not introduce a new type. In addition, I want the
naked expression "b = a" to error out during compile time.
The solution that I have implemented looks like this:
#define DECLARE_UNIQUE_CLASS_ALIAS(_ALIAS, _BASE)\
struct _ALIAS\
{\
_BASE value;\
_ALIAS(const _BASE& v)\
{\
this->value = v;\
}\
operator _BASE() const\
{\
return this->value;\
}\
};
DECLARE_UNIQUE_CLASS_ALIAS(A, int)
DECLARE_UNIQUE_CLASS_ALIAS(B, int)
This has the desired effect, but I am not happy with it because:
1. It uses macros, and is not debuggable.
2. Since the machine code for each alias is identical, and this will
be used a lot, this code will not be optimized by the compiler.
Could someone could suggest a solution that does not have the defects
that my code has?