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

David Krauss david_work at me.com
Sat Sep 13 16:24:22 CEST 2014


On 2014–09–13, at 9:52 PM, John Zwinck <jzwinck at gmail.com> wrote:

> I would appreciate any thoughts on this, and hope I have come to the right place to discuss it.

You have certainly come to the right place.

The problem can’t be solved in general, because member references generate a case where mutual recursion is possible:

struct s {
int & a = b;
int & b = a;
};

Globals allow similar evil:

// a.cpp
extern int & b;
int & a = b;

// b.cpp
extern int & a;
int & b = a;

Any solid rule to forbidding self-initialized references would need to have exemptions for such cases, which would be a serious devaluation. It comes down to QOI.

However, I think there is a problem that such programs are well-formed but only produce UB at runtime. The compiler should be allowed to complain that the reference is initialized without a referent object.

So, a good specification would be that a program is ill-formed but no diagnosis is required, if a reference initializer never refers to an object.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.open-std.org/pipermail/ub/attachments/20140913/09700276/attachment.html 


More information about the ub mailing list