[Bug c++/986] g++ misses warning for & on temporary

2010-08-27 Thread driscoll at cs dot wisc dot edu


--- Comment #28 from driscoll at cs dot wisc dot edu  2010-08-27 22:53 
---
> Your two functions are well defined as the scope of the temp is only lost
> after going out of scope.

I see "A reference is bound to a temporary object: the temporary object is
destroyed at the end of the reference's lifetime" at
<http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr382.htm>
(as well as 12.2 para 5 in the standard) now... that's something I totally did
not know.

Thanks, and sorry for the spam.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=986



[Bug c++/986] g++ misses warning for & on temporary

2010-08-27 Thread driscoll at cs dot wisc dot edu


--- Comment #26 from driscoll at cs dot wisc dot edu  2010-08-27 22:02 
---
I was surprised to see this is not caught by a warning as well. (The discussion
of whether it should be an error is silly; it pretty clearly shouldn't be.
There's -Werror if you disagree.)

Motivation: This surfaced in the code I'm working on as it has several similar
functions that return std::sets of objects. I changed several of them to return
by const-reference for efficiency purposes, but not all of them could be
modified such. I then changed call sites so that the return was stored in a
const-reference instead of copied. I expected the compiler to be able to catch
any mismatches (storing the by-value return in a reference), but when I ran a
test to see if it would, there was no warning.

Examples:

This first one is inspired by the code I was working on:
  int retByVal() {
  return 5;
  }
  int foo() {
  int const & v = retByVal();  // no warning
  return v;// no warning
  }
but you don't even need to get that complicated. The following illustrates as
well:
  int foo() {
  int const & x = 4;  // no warning
  return x;   // no warning
  }


Comparison of compilers:
- GCC fails to warn for either examples:
- 3.3.3, 3.4.6 (-Wall -W)
- 4.1.2, 4.5.1 (-Wall -Wextra)
- MSVC fails to warn for either of these examples
- 2005, 2008 (level 4)
- 2010 (all warnings, /Wall)
- The EDG front end warns if remarks are on:
- Intel CC (10.1 -Wall),  *does* warn about these examples. E.g.:
remark #383: value copied to temporary, reference to temporary used
int const & v = retByVal();
- Comeau (with -r) warns


I haven't lost debugging time to this or anything like that, but I am a bit
disappointed and surprised to see that neither MSVC nor GCC warns for this
almost-certain error and thought I'd contribute in the hopes that another
person running across it might inspire someone to fix it.


-- 

driscoll at cs dot wisc dot edu changed:

   What|Removed |Added

     CC|            |driscoll at cs dot wisc dot
       ||edu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=986