This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: bug in GCC or C++ standard ?


On 12/11/2008, René Bürgel <r.buergel@unicontrol.de> wrote:
> If all members of the union are const, why don't you just make the union
> itself const?

The const for the union seems to be ignored (code below).  The
original reason behind the union shenanigans was to provide a
compile-time alias to another variable of the same type (due to fusion
of two large legacy code bases into one). It is of course possible to
provide an alias in the form of const int&, however I tried to avoid
the storage penalty.

struct my_class_3
  {
  const union
    {
    int x;
    int y;
    };

  my_class_3() : x(0) {}
  };

int main(int argc, char** argv)
  {
  my_class_3 abc;
  abc.y = 5;
  std::cout << abc.x << std::endl;
  return 0;
  }

output:
5


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]