Literal ints are non-const?
Could someone please explain to me why a literal integer is an rvalue of type int instead of an rvalue of type const int?
In particular, it creates this strange situation:
Code:
template <typename T>
void foo(T&) { }
int main()
{
foo(3); // error
foo("asdf"); // ok
return 0;
}
Error message:
Code:
literalIntTest.cpp: In function ‘int main()’:
literalIntTest.cpp:6: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’
literalIntTest.cpp:2: error: in passing argument 1 of ‘void foo(T&) [with T = int]’
I realise that I can do:
Code:
template <typename T>
void foo(const T&) { }
But why aren't literals const?
I suppose maybe this is a good thing though as it encourages const-qualification where possible in templates?