I'm using Visual Studio Express 2013 and is fooling around a bit trying to learn about different things in C++.
I stumbled upon an interesting bug in the compiler where it doesn't seem to create a temporary object when explicitly type casting to the same type as the reference.
#include <iostream>
using namespace std;
int main()
{
int number; // float number;
number = 2;
const int& plainref_i = number;
const int& recastref_i = (int)number; // this goes wrong if number is int
const float& plainref_f = number;
const float& recastref_f = (float)number; // this goes wrong if number is float
number = 3;
std::cout << plainref_i << "\n";
std::cout << recastref_i << "\n";
std::cout << plainref_f << "\n";
std::cout << recastref_f << "\n";
return 0;
}
This will when compiled in VS, results in the following output: 3 3 2 2
But compiled with gcc, results in the following output: 3 2 2 2
If I replace "int number;" with "float number;" I get in VS: 2 2 3 3
and with gcc: 2 2 3 2
I'm wondering if anyone can confirm this as a bug and if anyone knows of a feasible workaround/solution.