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?
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?