I am working on big code base. While refactoring some code I found some strange behavior related to reference. Below is minimal code to explain my question.
#include <iostream>
int main() {
// case 1
{
int start = 1;
const int &eRef = start > 0 ? start : 9;
start = 2;
std::cout << start << eRef;
}
std::cout << std::endl;
// case 2
{
int start = 1;
int end = 9;
const int &eRef = start > 0 ? start : end;
start = 2;
std::cout << start << eRef;
}
std::cout << std::endl;
return 0;
}
Output of above code is
21
22
I am expecting output 22
for both case. But getting different output for both case.
It might be as per standard, but I would like to know reason behind it.