Hi, On Friday 25 February 2011 16:26:24 Richard Guenther wrote: > On Fri, Feb 25, 2011 at 4:19 PM, Matthias Kretz wrote: > > My expectation was, that, since the ctor has a constructed object as > > return value, the compiler is free, instead of calling a ctor twice for > > the case of e.g. > > Foo a(1); > > Foo b(1); > > , to do a memcopy of a to b after the a ctor is done. > > > > Instead the result of using __attribute__((const)) is, that GCC feels > > free to optimize the whole ctor away. Apparently the ctor is treated as > > a function that returns void. > > If you lie to the compiler weird things will happen ;)
I fully understand why it happened. So I imply your answer is that ctors do not have a return value and my expectation, as explained above, is wrong. > > Do you rather want a bug report for the website documenting attribute > > const, or for how GCC implements it? :-) > > None of the two. Ah, but if the semantics are meant to be this way then at least add another warning statement so that nobody else has to do a 2 hour bug-search session like I just did. The website says: "Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute below, since function is not allowed to read global memory. Note that a function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It does not make sense for a const function to return void." All of these statements hold for my ctor in question: 1. only examines its arguments 2. no effects other than writing to its class members 1b. doesn't read global memory 3. has an implicit pointer argument (this), which points to the memory area that is basically the return value of a ctor. 4. doesn't call any non-const function 5. there's no void anywhere in the function :-P So please change e.g. the last sentence to: "It does not make sense to use const with a function that returns void or with constructors." Regards, Matthias -- Dipl.-Phys. Matthias Kretz http://compeng.uni-frankfurt.de/index.php?id=mkretz