almost newbie like question regarding type conversion

Discussion in 'C++' started by Anton Lauridsen, Dec 5, 2004.

  1. 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?
     
    Anton Lauridsen, Dec 5, 2004
    #1
    1. Advertising

  2. Anton Lauridsen

    adbarnet Guest

    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" <antoncl do not spam me at hotmail> wrote in message
    news:41b2d919$0$216$...
    > 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?
    >
    >
    >
    >




    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    adbarnet, Dec 5, 2004
    #2
    1. Advertising

  3. Yep .. thank you

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


    "adbarnet" <> wrote in message
    news:41b2dda9$1_2@127.0.0.1...
    > 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" <antoncl do not spam me at hotmail> wrote in message
    > news:41b2d919$0$216$...
    >> 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?
    >>
    >>
    >>
    >>

    >
    >
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com
     
    Anton Lauridsen, Dec 5, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jérôme de Lagausie
    Replies:
    3
    Views:
    366
    Rob Thorpe
    Feb 11, 2004
  2. Patrick Kowalzick
    Replies:
    5
    Views:
    485
    Patrick Kowalzick
    Mar 14, 2006
  3. Daniel Pitts

    Almost Useful: Java Type Intersection

    Daniel Pitts, Nov 23, 2007, in forum: Java
    Replies:
    2
    Views:
    624
    Daniel Pitts
    Nov 23, 2007
  4. J Bondo
    Replies:
    5
    Views:
    800
    J Bondo
    Jan 19, 2009
  5. M P
    Replies:
    1
    Views:
    97
Loading...

Share This Page