0

Expected output : 5

Doesn't want to create a reference to itself.

#include <iostream>

int x = 3;

int main()
{
    int x = 5;
    {
         int &x = ::x;    // how to create a reference to int x = 5;
         std::cout << x << '\n';
    }
}
Jitu DeRaps
  • 144
  • 1
  • 9
  • *i was expecting 5* -- Why? [Did you have the compiler warnings on?](https://godbolt.org/z/Y48aj5vKb). – PaulMcKenzie Aug 03 '22 at 04:38
  • Your innermost `x` shadows the two outer `x`s, just as the middle `x` shadows the outermost `x`. – Nathan Pierson Aug 03 '22 at 04:39
  • Dupe: [What if a reference is declared to point to itself? E.g. int& x = x;](https://stackoverflow.com/questions/63459269/what-if-a-reference-is-declared-to-point-to-itself-e-g-int-x-x) – Jason Aug 03 '22 at 04:48

2 Answers2

3

This is undefined behavior. When you initialize the reference, the symbol x is already overloaded to refer to the reference.

If you enable all warnings you'll get something like this (gcc -Wall):

<source>: In function 'int main()':
<source>:9:15: warning: reference 'x' is initialized with itself [-Winit-self]
    9 |          int &x = x;
      |               ^
<source>:7:9: warning: unused variable 'x' [-Wunused-variable]
    7 |     int x = 5;
      |         ^
<source>:9:15: warning: 'x' is used uninitialized [-Wuninitialized]
    9 |          int &x = x;
      |               ^
<source>:9:15: note: 'x' was declared here
    9 |          int &x = x;
      |               ^
paddy
  • 60,864
  • 6
  • 61
  • 103
  • why it is behaving different if we didn't define a ```int x = 5``` ? [output :] https://godbolt.org/z/39oooPT1a – Jitu DeRaps Aug 03 '22 at 05:14
  • That regrettably is what undefined behaviour means. Your program isn't correct (x is used unitialized) so it may give you any output it wants. In general just be sure to compile your code without warnings, they really do matter. – Pepijn Kramer Aug 03 '22 at 05:21
  • @PepijnKramer ok, then why isn't compiling ? code link is mention in above comments ? – Jitu DeRaps Aug 03 '22 at 05:23
  • 1
    @JituDeRaps *Undefined behavior means anything can happen including but not limited to the program giving your expected output. But never rely on the output of a program that has UB. The progam may just crash.* – Jason Aug 03 '22 at 05:43
  • 1
    @JituDeRaps C++ It is one of the features of C++, it allows for very fine control but it also expects you (as a developer) to know what you are doing. And C++ expects you to initialize your variables. For example setting memory to 0 you are going to overwrite is extra work and C++ is not going to do that implicitly for you. Where possible C++ gives warnings (as it does in this case). For the rest you will have to write unit tests, and use code analysis tools. – Pepijn Kramer Aug 03 '22 at 08:01
-2

Because when you declare int &x = x; you initialise a variable x with itself, as it shadow the other x variables.

You get 1 but could get anything.

try int &y = x; instead

Alois Christen
  • 543
  • 3
  • 14