17

§8.5/6 in draft N3797 says:

To zero-initialize an object or reference of type T means:

  • ...
  • if T is a reference type, no initialization is performed.

I'd like to have an example of a reference that is zero-initialized.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Alexander
  • 2,581
  • 11
  • 17
  • huh? what? that is not possible – BЈовић Jan 21 '14 at 16:15
  • @yngum: But how can a reference be uninitialized is probably the OPs questiont hen – PlasmaHH Jan 21 '14 at 16:16
  • @BЈовић: if so then the follow-up question is, "why does the standard contain language to cover an impossible case?". And if not then the follow-up question is, "why does the standard permit a nonsensical case?" :-) – Steve Jessop Jan 21 '14 at 16:17
  • 1
    @juanchopanza he means [dcl.init]/5 – BЈовић Jan 21 '14 at 16:18
  • @SteveJessop The point is "if T is a reference type, no initialization is performed.". I am not an English native speaker, but I interpret that as "unitialized". I am not sure why they put it there, but g++ 4.8.1 throws an error trying to compile this `int & a{};` – BЈовић Jan 21 '14 at 16:22
  • `using type = int&; type();` – Simple Jan 21 '14 at 16:23
  • 1
    Note, this [answer](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents/4653479#4653479) provides links to many of the draft standards. – Shafik Yaghmour Jan 21 '14 at 17:01
  • @ShafikYaghmour Very useful. Tks. – Alexander Jan 21 '14 at 17:47
  • Since references are always initialised to an explicit value, isn't this just saying that in cases where a variable gets set to zero (like static), a reference does not? Clunky language, but the meaning seems clear enough to me. – david.pfx Jan 23 '14 at 13:38

1 Answers1

16

Later the standard says "Every object of static storage duration is zero-initialized at program startup before any other initialization takes place." So if you have e.g. in global scope

int x;
int& r = x;

r is first zero-initialized, and then initialized by x. For reference, zero-initialized means nothing, so it is mere technicality.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • Note that a reference is *not necessarily* an object. An object is a region of storage, but it's unspecified whether a reference requires any storage. This doesn't change the validity of your answer though. – Joseph Mansfield Jan 21 '14 at 16:28
  • 1
    @JosephMansfield Make it a class type with a reference member. – dyp Jan 21 '14 at 16:28
  • 2
    @JosephMansfield In Standardese references and objects are distinct. The precise wording that Wojtek is alluding to goes (emphasis mine): ‘**Variables** with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.’ Variables can have a reference type. – Luc Danton Jan 21 '14 at 16:50