Joe Buck <[EMAIL PROTECTED]> writes: | On Thu, Jan 11, 2007 at 04:09:16AM +0100, Gabriel Dos Reis wrote: | > The subtlety I'm refering to is not that "void* p = &p" is not well-defined, | > but rather the fact that when we see | > | > T t = some-expression-involving-t; | > | > we would like to warn for cases where there is a high probability that | > the *initialization* of "t" *results in undefined behaviour*, as opposed | > to leaving "t" undefined. -Wunintialized was not designed to handle | > those cases. That matter is compounded by the fact that | > some constructs such as | > | > circular_buffer buf = buf; | > | > are well-formed and not attempt to work around agreed deficiency of | > -Wunitialized. To do that, it is not clear -- without seeing the body | > of the copy constructor -- whether only the address is used or not. | | There are three cases: either you can be certain that an uninitialized | value will be used, or you can be certain that it won't be used, or | you don't know because you don't see the body of the copy constructor. | | Case 1: | int i = i; | or | SomeClass p = p; // compiler-generated copy constructor | Case 2: | void* p = &p; | Case 3: | SomeClass p = p; // user-defined copy constructor, can't see the body | | Case 2 is completely valid. In Case 1 we have uninitialized variables. | In Case 3 we cannot tell. | | There's an argument for not warning in case 3, though unfortunately | in my early days of C++ programming I often managed to make mistakes | similar to this, and the compiler would not warn.
I would like the compiler to warn for case 1 when the copy-constructor is inline, along with cases like int i = 2 * i; There are comparable, relatively simple cases that the compiler can warn about without requiring optimization be turned on. -- Gaby