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

Re: Hiding "std"

53 views
Skip to first unread message
Message has been deleted

Christian Gollwitzer

unread,
May 31, 2015, 2:38:12 AM5/31/15
to
Am 31.05.15 um 00:46 schrieb Stefan Ram:
> Before I tried it, I was not sure whether the following
> program would compile and how it would behave.
>
> #include <iostream>
> #include <ostream>
>
> int main()
> { int std = 3;
> std::cout << std << '\n'; }
>
>

I can't find a problem here. In C++, namespaces and variables live in a
different domain. This would be different in Python, where a module is
actually an object and can be passed around and assigned to. But in C++
there is no way to do such a thing, as in the fictional code below.

namespace mystd=::std;
void doitthere(namespace where) {
where::cout<<"Something";
}

doitthere(mystd);




Christian

Marcel Mueller

unread,
May 31, 2015, 3:11:34 AM5/31/15
to
On 31.05.15 00.46, Stefan Ram wrote:
> #include <iostream>
> #include <ostream>
>
> int main()
> { int std = 3;
> std::cout << std << '\n'; }

::std::cout << std

fixes the name clash.


Marcel

Öö Tiib

unread,
May 31, 2015, 8:15:11 AM5/31/15
to
There are none to fix. That int std can not be considered as
class-name-or-namespace-name so it does not participate as one in
construction of qualified-id 'std::cout'.

In general such code is useful as a test case for a parser of C++
or as element of obfuscation.
It would be lot easier for everybody if the compiler said that the name
'std' is taken already and suggested to use some other for that int.
0 new messages