Consider the following code:
template <typename T>
void foo(const T& param) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
std::cout << param;
}
int main()
{
foo<const int&>(5);
return 0;
}
The output gives:
void foo(const T&) [with T = const int&]
5
Here apparently T is resolved to const int& (as the printout says, since I explicitly forced so). However, this seems to create a problem: The function signature takes a parameter of type const T&, and in this case it would expand to const const int& &, which is not legal syntax in C++.
But the program indeed runs fine. What's going on here?
Update:
I know that T& & would collapse to T&. However, in this case, the double const is also not legal if you write them explicitly, and I don't see that template collapsing rule mention this part.
Update 2:
Compiling const const int x = 1; gives error: duplicate ‘const’. Multiple const is not allowed in c++ (but surprisingly in c it's okay).