This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/986] g++ misses warning for & on temporary
- From: "driscoll at cs dot wisc dot edu" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 27 Aug 2010 22:02:45 -0000
- Subject: [Bug c++/986] g++ misses warning for & on temporary
- References: <bug-986-184@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- 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