Discovered this by accident today after not paying attention to my find & replace commands in vim...
#include "iostream"
int main()
{
int number{number}; // wtf ?
std::cout << "number=" << number << std::endl;
}
Compiling with g++ -Wall
produces the warning
main.cpp: In function ‘int main()’:
main.cpp:5:22: warning: ‘number’ is used uninitialized in this function [-Wuninitialized]
int number{number};
as hoped.
Running the program produces the output
number=0
What the heck is going on here?
- How can I create the variable
number
and initialize it to "number" - a variable which does not exist yet? - Does this produce undefined behaviour?
- Is this a bug in gcc?
- Is it not a bug for some technical reason*, but perhaps should it be considered to be something that you shouldn't be allowed to do? (If the rules of the syntax allow for this line of code to be valid, then perhaps should this be considered an exception and thus be illegal code?)
- * If technical reason: What is the technical reason? How does gcc interpret the line of code
int number{number}
?