By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

Creating mutually non-convertible yet identical classes

P: n/a
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?

Nov 3 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
re****@gmail.com wrote:
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.
[..]

Could someone could suggest a solution that does not have the defects
that my code has?
Use enums, maybe?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 3 '07 #2

P: n/a
On Nov 3, 9:22 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
red...@gmail.com wrote:
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.
[..]
Could someone could suggest a solution that does not have the defects
that my code has?

Use enums, maybe?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
I think for some cases, enums will work fine. The problem with them is
that you are at the mercy of the compiler's internal representation of
enum. In particular, if the integral type to be overloaded is int64
and the compiler maxes enum out to 32 bit precision, the code will
fail.
Also, the enum solution only works for integers and can not be
generalized to any other type. I was hoping for some generic template,
non-macro based solution.

Nov 3 '07 #3

P: n/a
re****@gmail.com wrote:
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.

How about something like

template <typename T, typename V>
struct Unique
{
V value;

Unique( const V& value = V() ) : value(value) {}

operator V() const { return value; }
};

struct A_; typedef Unique<A_,intA;
struct B_; typedef Unique<B_,intB;

void f(A a);
void f(B b);

int main() {
A a;
B b;

a = 21; // OK
a = b; // Error
}

--
Ian Collins.
Nov 3 '07 #4

P: n/a
On Nov 3, 11:24 am, Ian Collins <ian-n...@hotmail.comwrote:
red...@gmail.com wrote:
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.

How about something like

template <typename T, typename V>
struct Unique
{
V value;

Unique( const V& value = V() ) : value(value) {}

operator V() const { return value; }

};

struct A_; typedef Unique<A_,intA;
struct B_; typedef Unique<B_,intB;

void f(A a);
void f(B b);

int main() {
A a;
B b;

a = 21; // OK
a = b; // Error

}

--
Ian Collins.
Thank you. This is precisely what I was looking for.

Nov 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.