C++ Logo

sg12

Advanced search

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

From: David Krauss <david_work_at_[hidden]>
Date: Sat, 13 Sep 2014 22:24:22 +0800
On 2014–09–13, at 9:52 PM, John Zwinck <jzwinck_at_[hidden]> 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.


Received on 2014-09-13 17:25:06