I am using Visual Studio Express 2013 and a little cheating when trying to learn about different things in C ++.
I came across an interesting error in the compiler, where it seems that a temporary object is not created when explicitly imposing a listing on the same type as the link.
#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, when compiled in VS, leads to the following result: 3 3 2 2
But compiled with gcc leads to the following result: 3 2 2 2
If I replaced "int number"; with "floating point number"; I get to VS: 2 2 3 3
and with gcc: 2 2 3 2
I am wondering if anyone can confirm this as an error, and if anyone knows of a possible workaround / solution.
c ++ casting visual-studio-2013 temporary-objects
dnfs
source share