auto
uses the same type deduction rules as templates. In your case auto x
means the same as a template type parameter.
template<typename T>
int function(T t) {
T x; // <-- is equivalent to `auto x` outside of a template
};
In your example the type is deduced to an int
rather than int&
as you were expecting which is why the original value remained the same. You will need to declare the reference explicitly (auto& x
) in order to modify the original value.