almost newbie like question regarding type conversion

  • Thread starter Anton Lauridsen
  • Start date
A

Anton Lauridsen

Hi

This is quite embarrsing, having 16+ years of experience in writing c/c++
code, and suddenly I have something I cannot figure out in the language.

Here is the deal:

I want to define two different simple integral types, which are incompatible
with each other, or atleast one simple integral type which is incompatible
with 'int', thus either triggering a compiler waring or error when assigning
between the types.

My problem is really due to bad coding (code I have inherited :)

the code operates on a structure like this:

struct Node {
int Id;
bla.
bla.
bla.
};

and arrays of struct Node:

struct Node data[300];

Here is the fun part, orginally there was no difference between an index
into the array and the value of Id, i.e. if the Id contained 17, then it
would always be located at data[17].

I am implementing a change, which causes this to be no longer true. Now the
code fails in 10 gazilion places, because it relies heavily on this
invariant, i.e. data[n.Id], in a variariety of subtle different ways are
scattered all over the code.

My plan is to replace the datatype of Id inside the Node struture with a
type and have the compiler find all the places where this invariant is
assumed, e.g.

typedef int NODEID;
struct Node {
NODEID Id;
bla.
bla.
bla.
}:

and then get an error when something like this happens;
for(int i=0; i<xxx; data.Id = i++);

any suggestions?
 
A

adbarnet

If you are just trying to catch all locations where NODEID.Id is
constructed, assigned, or incremented at compile time, then you could write
a class which dis-allows these methods (declare them private and don't
implment them). That way the compiler will give you each location where it
happens.

If you want the run-time behaviour to throw an exception, then implement the
methods and have them throw.


Anton Lauridsen said:
Hi

This is quite embarrsing, having 16+ years of experience in writing c/c++
code, and suddenly I have something I cannot figure out in the language.

Here is the deal:

I want to define two different simple integral types, which are
incompatible with each other, or atleast one simple integral type which is
incompatible with 'int', thus either triggering a compiler waring or error
when assigning between the types.

My problem is really due to bad coding (code I have inherited :)

the code operates on a structure like this:

struct Node {
int Id;
bla.
bla.
bla.
};

and arrays of struct Node:

struct Node data[300];

Here is the fun part, orginally there was no difference between an index
into the array and the value of Id, i.e. if the Id contained 17, then it
would always be located at data[17].

I am implementing a change, which causes this to be no longer true. Now
the code fails in 10 gazilion places, because it relies heavily on this
invariant, i.e. data[n.Id], in a variariety of subtle different ways are
scattered all over the code.

My plan is to replace the datatype of Id inside the Node struture with a
type and have the compiler find all the places where this invariant is
assumed, e.g.

typedef int NODEID;
struct Node {
NODEID Id;
bla.
bla.
bla.
}:

and then get an error when something like this happens;
for(int i=0; i<xxx; data.Id = i++);

any suggestions?
 
A

Anton Lauridsen

Yep .. thank you

I got so focussed on trying to solve the issue with a typedef rather than a
class.


adbarnet said:
If you are just trying to catch all locations where NODEID.Id is
constructed, assigned, or incremented at compile time, then you could
write a class which dis-allows these methods (declare them private and
don't implment them). That way the compiler will give you each location
where it happens.

If you want the run-time behaviour to throw an exception, then implement
the methods and have them throw.


Anton Lauridsen said:
Hi

This is quite embarrsing, having 16+ years of experience in writing c/c++
code, and suddenly I have something I cannot figure out in the language.

Here is the deal:

I want to define two different simple integral types, which are
incompatible with each other, or atleast one simple integral type which
is incompatible with 'int', thus either triggering a compiler waring or
error when assigning between the types.

My problem is really due to bad coding (code I have inherited :)

the code operates on a structure like this:

struct Node {
int Id;
bla.
bla.
bla.
};

and arrays of struct Node:

struct Node data[300];

Here is the fun part, orginally there was no difference between an index
into the array and the value of Id, i.e. if the Id contained 17, then it
would always be located at data[17].

I am implementing a change, which causes this to be no longer true. Now
the code fails in 10 gazilion places, because it relies heavily on this
invariant, i.e. data[n.Id], in a variariety of subtle different ways are
scattered all over the code.

My plan is to replace the datatype of Id inside the Node struture with a
type and have the compiler find all the places where this invariant is
assumed, e.g.

typedef int NODEID;
struct Node {
NODEID Id;
bla.
bla.
bla.
}:

and then get an error when something like this happens;
for(int i=0; i<xxx; data.Id = i++);

any suggestions?

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top