On Sunday, 18 December 2016 13:38:41 UTC+2, Paul wrote:
> The code below outputs 5 -3 using gcc C++ 11 (exact details of compiler
> omitted).
> When I write int min = -3; I would expect the compiler to interpret this
> as meaning (int std::min = 3). Since that doesn't make any sense, I would
> expect a compilation error.
C++ name lookup is so complex that major compiler vendors struggled long
time to implement it. Such basics like you ask about were OK but I knew
at least one name lookup defect in each until 2008 or so.
> Is it a rule of C++ that variable definitions are allowed to clash
> with function names? Or is the below code not robust to all compilers?
The names 'min' are from different scope so there are no clash. Instead
name from local scope hides the less local name. Let me vandalize your
code to explain:
#include <iostream>
#include <algorithm>
using namespace std; // <- you dig your own hole here
int main()
{
cout << min(5,7) << " ";
int min /* from here std::min is hidden */ = -3;
// so we can't use it unqualified anymore:
// cout << min(42,7) << " ";
cout << std::min(42,7) << " "; // <- we have to qualify it
cout << min << std::endl;
return 0;
}
However ... why to name several things with same name? Did you
run out of names?