7

Consider some code:

#include <iostream>

int main()
{
    using std::cout;
    int a=3;
    cout << "a="<<a<<"\n";

    {
        int a=a;
        cout << "new a = " << a << "\n";
        a=5;
        cout << "a = " << a << "\n";
    }
    cout << "old a = " << a << "\n";
}

I'd expect it to print

a=3
new a = 3
changed a = 5
old a = 3

But what I get actually appears to say new a = 0 in the second line. I thought that it would work like initialization list in a class' constructor, where one can write like

C::C(int a) : a(a) {}

But for some reason this is different. First, removing the outer code completely doesn't result in a compilation error. So I assume that int a=a; is valid. Turning on all the compiler warnings leads to this:

test.cpp: In function ‘int main()’:
test.cpp:10:15: warning: ‘a’ is used uninitialized in this function
         int a=a;

So my question now: why is this syntax valid at all? Why doesn't the compiler say something like "undefined variable a"?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Ruslan
  • 18,162
  • 8
  • 67
  • 136
  • 6
    Should read [Does initialization entail lvalue-to-rvalue conversion? Is `int x = x;` UB?](http://stackoverflow.com/q/14935722/1708801) and [Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++14?](http://stackoverflow.com/q/23415661/1708801) – Shafik Yaghmour Jan 26 '15 at 14:43
  • 1
    @101010 [Ahem?](http://coliru.stacked-crooked.com/a/20752252aa38e7c1) – Borgleader Jan 26 '15 at 14:44
  • @ShafikYaghmour If you posted that as a answer along with a short gist, you'd get some votes and this question could be marked as answered. – Björn Pollex Jan 26 '15 at 14:45
  • 1
    @BjörnPollex it is a duplicate, very close to the second link I posted but there is probably a closer duplicate someplace but it should be closed as a duplicate none the less. – Shafik Yaghmour Jan 26 '15 at 14:51

1 Answers1

13

It's syntactically valid, since the variable's point of declaration comes before its initialiser, and the name is available anywhere after that point. This allows less dodgy initialisations like

void *p = &p;

which legitimately uses the name (but not the value) of the variable being initialised.

It's behaviourally invalid, since using the value of an uninitialised object gives undefined behaviour. That's not an error that requires diagnosis (since, in general, it can be difficult or impossible to analyse the program flow to see whether an object has been initialised), but as you note, many compilers will give a warning for straightforward cases like this.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644