f



Legal name clash?

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.
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?

Thank you,
Paul


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    cout << min(5,7) << " ";
    int min = -3;
    cout << min;
    return 0;
}
0
Paul
12/18/2016 11:38:31 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

3 Replies
617 Views

Similar Articles

[PageSpeed] 17

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?
0
UTF
12/18/2016 2:11:21 PM
On 2016-12-18 15:11, Öö Tiib wrote:
> 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?
>

Like you say, having a "using namespace std;" in the code REALLY uses up 
a lot of names. The section "Index of library names" is 55 pages in the 
latest C++17 draft.

Adding all of those to your own code might cause all kinds of problems.



     Bo Persson

0
Bo
12/18/2016 2:27:45 PM
[Please do not mail me a copy of your followup]

Paul <pepstein5@gmail.com> spake the secret code
<3dd34a81-f38d-4923-a417-c4665d24f666@googlegroups.com> thusly:

>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).

Why would you expect this?

'using namespace std' makes the namespace std available for looking up
names.  It doesn't put newly declared names inside the namespace
'std'.

There's nothing wrong with this code:

>#include <iostream>
>#include <algorithm>
>using namespace std;

This tells the compiler "when I use a name that I haven't declared, go
look for it in namespace 'std' and see if it is declared there."

>int main()
>{
>    cout << min(5,7) << " ";
>    int min = -3;

This declares a local variable names 'min' of type 'int'.

>    cout << min;

Names from the local scope are always preferred before looking in any
namespaces added with a 'using namespace' statement.

>    return 0;
>}
-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
            The Terminals Wiki <http://terminals-wiki.org>
     The Computer Graphics Museum <http://computergraphicsmuseum.org>
  Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
0
legalize
12/19/2016 7:11:17 PM
Reply: