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: a warning to implement


> -----Original Message-----
> From: Gabriel Dos Reis [mailto:gdr@codesourcery.com]
> Sent: Wednesday, February 06, 2002 5:40 PM
> To: nathan@cs.bris.ac.uk
> Cc: Robert Dewar; gdr@codesourcery.com; rsandifo@redhat.com;
> gcc@gcc.gnu.org
> Subject: Re: a warning to implement
> 
> 
> 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.

I think here we should REALLY had a warning; in fact you have here a class
which explicitely do NOT have a default constructor, and then we create an
un-constructed object value. 

OTOH, if I have

class Node {
      Node* link;
public:
	Node() { link = 0; }
	~Node() { delete link; }
	Node(Node* n) : link(n) { }
	Node(Node& n) : link(&n) { }
};

int main() {
	T x = x;
}

Then I' most surely have, in my Node class, taken all possible precautions
to avoid any use of undefined pointer values, yet the given main will most
surely crash with some kind of SIGSEGV ;-(

So IMNSHO, the warning IS needed and should be on ASAP, so at least in
-Wall.

Just my .02euro

	Bernard

--------------------------------------------
Bernard Dautrevaux
Microprocess Ingenierie
97 bis, rue de Colombes
92400 COURBEVOIE
FRANCE
Tel:	+33 (0) 1 47 68 80 80
Fax:	+33 (0) 1 47 88 97 85
e-mail:	dautrevaux@microprocess.com
		b.dautrevaux@usa.net
-------------------------------------------- 


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