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]

Reference-to-pointer argument bug?


Thought I'd see if I'm thinking straight before I file a bug report...

Shouldn't the following be illegal?  (It compiles fine under g++ 3.3.1.)

  void func(void *&c) {
      c = new char[8];
  }

  int main() {
      char *v = 0;
      func((void *)v); // ILLEGAL: non-const reference to temporary void*
      cout << "v is " << (void*)v << endl;

      void *w = 0;
      func(w);
      cout << "w is " << w << endl;
  }

The call to func is implicitly creating a temporary void* and passing a
*non-const* reference to it.  The assignment "c = new char[8]" appears to
set the value of the temporary rather than v; the program prints out "v is
0."  If the signature of func were "void func(void * const &c)" this would
be permissible, but not for a non-const ref (I think); the compiler should 
report an error.

Am I misunderstanding something or is the compiler failing to report an 
error?

- Jon


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