Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 31,454 of 33,346    |
|    Francis Glassborow to Javier Jimenez    |
|    Re: Reference to myself compiles... and     |
|    06 Sep 11 12:32:25    |
      a896bd18       From: francis.glassborow@btinternet.com              On 05/09/2011 23:20, Javier Jimenez wrote:       > Hello       >       > The example code bellow compiles in gcc, and produces a core dump.       > When I noticed the error ( int& i = i; ), I would expected two       > possible compiler behaviours:       > - it does not compile.              Perfectly reasonable expectation and many years ago (about 1996) I       suggested that       int i = i;              self initialisation, be deemed an error. However there is an obscure       program technique that relies on this declaration being valid for a user       defined type and so WG21 were reluctant to remove it. However good tools       will identify it and warn you.              Now I know that what you wrote is not the same but I falls into a       similar parsing problem. Once i has been declared it will hide any outer       declaration of i. However I think that there is no reason why this form       of declaration should not be marked as ill-formed.              > - the reference points to the "i" out of the "if context" (const int       > i)              Yes, that would be reasonable but it would also be inconsistent so I       would prefer it to be ill-formed.       >       > But my surprise is that it does compile... and crashes in runtime.       >       > Why?              Why does it compile? Probably because the compiler does not identify it       as erroneous That is probably because the C++ Standard does not       explicitly outlaw it though I suspect that it is actually ill-formed       implicitly because the reference does not refer to storage.              int i;       int & j = i;       int & k = j;              is, I think, well formed and valid. We would need to explicitly require       that a reference was initialised to actual storage (though possibly in a       register) even if the compiler has to see through a number of       intermediate references. Actually wording that is not simple and, I       think, has to be pulled out as a special case. I.e.              type t& = t;       and       type t&(t);              would have to be made ill formed.              Try posting your example to comp.std.c++                     --        [ See http://www.gotw.ca/resources/clcm.htm for info about ]        [ comp.lang.c++.moderated. First time posters: Do this! ]              --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca