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:
Error message:Code:template <typename T> void foo(T&) { } int main() { foo(3); // error foo("asdf"); // ok return 0; }
I realise that I can do: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]’
But why aren't literals const?Code:template <typename T> void foo(const T&) { }
I suppose maybe this is a good thing though as it encourages const-qualification where possible in templates?



LinkBack URL
About LinkBacks



