I stumbled upon some code today, I simplified it to this :
#include <iostream>
using std::cout;
using std::cin;
bool changeX(int &x)
{
x = 5;
return true;
}
void printvals(bool bval, int intval)
{
cout << bval << " : " << intval;
}
int main()
{
int x;
printvals(changeX(x), x);
cin.get();
}
Here, x
is still uninitialized at the time it's passed to the function printvals
but can I say for sure that x will always be initialized before printvals uses it?
I tried to run my simplified code in VS2013 debug-mode which gave me : Run-Time Check Failure #3 - The variable 'x' is being used without being initialized.
. However, running it in release-mode ran fine and printed : 1 : 5
as expected.
Does this meant that I can use this approach in production code? Will x
always be initialized before printvals
can use it so it doesn't cause UB?