C++ Logo


Advanced search

Re: [ub] Proposal: make self-initialized references ill-formed (C++17?)

From: Jens Maurer <Jens.Maurer_at_[hidden]>
Date: Sun, 21 Sep 2014 14:27:43 +0200
On 09/18/2014 09:42 PM, Richard Smith wrote:
> On 13 September 2014 06:52, John Zwinck <jzwinck_at_[hidden] <mailto:jzwinck_at_[hidden]>> wrote:
> I recently happened upon some code which, boiled down to its essence, was like this:
> | for (int ii = 0; ii < 1; ++ii)
> {
> const std::string& str = str; // !!
> std::cout << str << std::endl;
> }|
> My to my surprise, this code compiled (and produced a segfault at runtime). I say surprise because I had all warnings enabled (as errors) in GCC 4.7 and 4.9, yet there was no complaint. I got a good answer from Jonathan Wakely (http://stackoverflow.com/a/25720743/4323) explaining why GCC failed to catch it, but this got me thinking: why does C++ allow this at all?
> So, a proposal: perhaps in C++17 we could declare that self-initialized references are ill-formed. I did consider whether this might impact existing code; the only use case that came to mind might be SFINAE, though I surely have never seen it used that way.
> FYI, this is core issue 504:
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#504

... which doesn't necessarily mean it will be automatically addressed by CWG
in the near future.

Feel free to write a short paper suggesting specific wording changes to address
this issue. (Or, if just 1-2 sentence, send your wording changes to Mike Miller
if you won't attend WG21 meetings in person.)


Received on 2014-09-21 14:32:56