Topic: separate casts for int vs unsigned int?


Author: vavasis@cs.cornell.edu (Stephen Vavasis)
Date: Fri, 5 Aug 1994 16:51:07 GMT
Raw View
There was a discussion recently about casts between int and unsigned
int; I don't remember what the outcome was, but now I realize that I
have this exact problem.  Specifically, the code below will compile
under g++ (gcc 2.5.8) but fails to compile under Sun C++ 3.0.1.  On
the other hand, if I comment out the line marked "questionable" then
it compiles for SunC C++ but it breaks g++!  In both cases the error
is about "ambiguous type conversion".

So it seems that I can't write this program to work for both compilers
-- is this true?  Which version of the program is legal according to
the "standard" or the "ARM"?  Also, assuming that I really do need two
different versions of my program for the two compilers, can someone
tell me what "#ifdef" statement I can use to test if the compiler is
gcc?

Thanks,
Steve Vavasis


#include <iostream.h>
typedef unsigned long int INT;
class Base3 {
  INT word;
public:
  Base3() { word = 0;}
  Base3(unsigned long int t) { word = t;}
  inline void operator++ () { word++;}
  inline operator INT() const {return word;}
  inline operator int() const {return (int) word;}  // Questionable line here
};

class vec5 {
  int entries[5];
public:
  inline int operator[](int i) const {return entries[i];}
  inline int& operator[](int i) {return entries[i];}
};

main() {
  vec5 x;
  Base3 i;
  for (i = 0; i < 5; ++i)
    x[i] = i * 7;
  for (i = 0; i < 5; ++i)
    cout << x[i] << " " ;
  cout << "\n";
  return 0;
}