Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Legal name clash?

44 views
Skip to first unread message

Paul

unread,
Dec 18, 2016, 6:38:41 AM12/18/16
to
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;
}

Öö Tiib

unread,
Dec 18, 2016, 9:11:42 AM12/18/16
to
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?

Bo Persson

unread,
Dec 18, 2016, 9:27:55 AM12/18/16
to
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

Richard

unread,
Dec 19, 2016, 2:11:24 PM12/19/16
to
[Please do not mail me a copy of your followup]

Paul <peps...@gmail.com> spake the secret code
<3dd34a81-f38d-4923...@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 new messages