a warning to implement

Gabriel Dos Reis gdr@codesourcery.com
Wed Feb 6 08:50:00 GMT 2002


Nathan Sidwell <nathan@acm.org> writes:

| Hi,
| I've been reading this thread, and I still can't figure out how
| 	T x = x;
| as anything but undefined semantics. I'm talking C++ here.
| [3.3.1]/1 gives an example about point of declaration, and says of
| 	int x = x;
| 	Here ... is initialized with its own (indeterminate) value.
| 
| But, that assignment requires an lvalue to rvalue conversion on 'x',
| and [4.1] says
| 	'or if the object is uninitialized,... undefined behaviour'

The object isn't uninitialized; it is initialized with an
indeterminated value. The point is precisely to give an explicit hint
to the compiler to assign (if possible) to assign a singular value to
x (pretty much in the same way you might assign a SNaN to a floating
point to prevent use before explicit assignment with determinate
value. 

| So it appears that we've got undefined behaviour for any POD T.

Except when T = char or unsigned char.

| For a class T, we could be calling the copy ctor T(T const &).
| Reference binding at [8.3.1]/4 says a reference must be bound to a
| `valid object'. I suppose that it is ok to reference bind to an
| object of indeterminate value.

The following is OK:

  struct Node {
     Node* link;
     Node(Node& n) : link(&n) { }
  };

  int main()
  {
     T x = x;
  }

and is a canonical example of use for user-defined-type.

-- Gaby



More information about the Gcc mailing list