In the simple following example, I expected the output would be "2222". But the actual output was "2122" for both VC++ 11.0 and g++ 4.6.1.
#include <iostream>
template <class T>
void func(T x)
{
x = 2;
std::cout << x;
}
int main()
{
int x = 1;
func((int &)x);
std::cout << x;
func<int &>(x);
std::cout << x;
return 0;
}
I disassembled and found that the first func call, func((int &)x), uses func<int> instead of func<int &>. Why and how is this happened?