Hello all,

I think the gcc compiler should not accept assignments to non-lvalues.
gcc does this however, but does not execute the assignment (probably the
assignment is executed on a copy, but that copy is entirely floating).

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

template<class T> class SuperArray
{
  T **block;
  int size;

public: 
  SuperArray() { block = (T **) calloc (32768, sizeof(T *)); if (!block)
exit (1); size = 0; }
  void Wipe() { int k; for (k=0; k<=size-1>>16; k++) free (block[k]);
size = 0; }
  ~SuperArray() { int k; for (k=0; k<=size-1>>16; k++) free (block[k]);
free (block); }
 
  T /*&*/operator[] (int index)
  {
    if (index < 0) exit (1); // range check for debugging
    if (index < size) return block[index>>16][index&65535];
    if (index > size) exit (1); // range check for debugging
    // size must increase
    int k;
    for (k=(size-1>>16)+1; k<=(index>>16); k++)
    {
      block[k] = (T *) calloc (65536, sizeof (T));
      if (!block[k]) exit (1);
    }
    size = index + 1;
    return block[index>>16][index&65535];
  }
  T *operator+ (int index)
  {
    if (index < 0) exit (1); // range check for debugging
    if (index < size) return block[index>>16] + (index&65535);
    if (index > size) exit (1); // range check for debugging
    // size must increase
    int k;
    for (k=(size-1>>16)+1; k<=(index>>16); k++)
    {
      block[k] = (T *) calloc (65536, sizeof (T));
      if (!block[k]) exit (1);
    }
    size = index + 1;
    return block[index>>16] + (index&65535);
  }                                         
};

struct string100 { char value[100]; };

main ()
{
  SuperArray<string100> a;
  strcpy ((a + 0)->value, "No assignment.");
  strcpy (a[0].value, "Non-lvalue assignment."); // illegal
  printf ("%s\n", (a + 0)->value);
} 



Reply via email to