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

Inject std namespace into main()

89 views
Skip to first unread message

parmenides

unread,
Sep 11, 2013, 11:28:23 AM9/11/13
to
Hi,

For the flowing code:

#include <iostream>

int cout;

int main()
{
using namespace std;
cout << endl;
return 0;
}

I think all symbols in std are injected into main(), that is , the cout is a
object local to the main(). It should shield the other cout in the global
namespace. But, the complier has told me:

reference to 'cout' is ambiguous

Does the symbols of std is not local to the main()?



parmenides

unread,
Sep 11, 2013, 11:33:08 AM9/11/13
to

Victor Bazarov

unread,
Sep 11, 2013, 11:38:03 AM9/11/13
to
I think the answer to that is "no", if you mean what I think you mean.
You make them available in 'main' without the 'std::' prefix, but you
don't re-declare them in 'main'. The _using_ directive is not a
declaration.

V
--
I do not respond to top-posted replies, please don't ask

Scott Lurndal

unread,
Sep 11, 2013, 11:56:48 AM9/11/13
to
No, it means that the compiler can't figure out which 'cout' to use, since
both would be legal in that context.

scott

Nobody

unread,
Sep 11, 2013, 12:58:27 PM9/11/13
to
On Wed, 11 Sep 2013 23:33:08 +0800, parmenides wrote:

> I think all symbols in std are injected into main(), that is , the cout is
> a object local to the main(). It should shield the other cout in the
> global namespace.

No. It makes std::cout visible as cout within main(). It doesn't hide any
other occurrences of that name, hence the error about cout being ambiguous.

Paavo Helde

unread,
Sep 11, 2013, 1:48:07 PM9/11/13
to
"parmenides" <mobile.p...@gmail.com> wrote in
news:l0q2eh$ako$2...@speranza.aioe.org:
No, the using directive does not do what you want. However, the using
*declaration* does:

#include <iostream>

int cout;

int main()
{
using std::cout;
using std::endl;
cout << endl;
return 0;
}

Alf P. Steinbach

unread,
Oct 29, 2013, 7:27:49 PM10/29/13
to
The short version is that the `using` directive causes name lookup to
look also in the specified namespace.

Apparently you envision an effect as if there were a local declaration
of `cout`. But the directive only adds additional name lookup places. A
`using` DECLARATION, on the other hand, has that as-if-local-declaration
effect, so that the above usage of `cout` (but not `endl`) will compile,
and so that another local declaration would be invalid:

[code]
#include <iostream>

int cout;

int main()
{
using std::cout; // A `using` declaration.
cout << endl;
}
[/code]

Note: the above fails to compile due to the use of unqualified `endl`.

This is a main difference between a using DIRECTIVE (which you employed)
and a using DECLARATION (above). The directive only directs lookup. The
declaration acts more like a declaration. :-)


Cheers & hth.,

- Alf

SG

unread,
Oct 30, 2013, 10:48:23 AM10/30/13
to
On Wednesday, October 30, 2013 12:27:49 AM UTC+1, Alf P. Steinbach wrote:
>
> This is a main difference between a using DIRECTIVE (which you employed)
> and a using DECLARATION (above). The directive only directs lookup. The
> declaration acts more like a declaration. :-)

As for how this "direction" works: It's like the names are injected
into the closest namespace level that contains both, the context in
which you are writing the directive and the namespace you are naming
in the directive. So, for example:

int i;
namespace foo {
namespace bar {
int i;
}
namespace baz {
using namespace bar; // puts the names from ::foo::bar
// "temporarily" into ::foo because
// foo is the "closest" namespace that
// contains both bar and baz.

void func() {i=23;} // i refers to ::foo::bar::i, it is
// found when lookup reaches the foo
// namespace.
}
void another() {i=42;} // i refers to ::i.
}

By "temporarily" I tried to refer to the scope in which the using
directive is "active".

HTH,
SG

SG

unread,
Oct 30, 2013, 10:56:27 AM10/30/13
to
On Wednesday, September 11, 2013 5:28:23 PM UTC+2, parmenides wrote:
> Hi,
>
> For the flowing code:
>
> #include <iostream>
>
> int cout;
>
> int main()
> {
> using namespace std;
> cout << endl;
> return 0;
> }
>
> I think all symbols in std are injected into main(), that is , the cout is a
> object local to the main(). It should shield the other cout in the global
> namespace. But, the complier has told me:
>
> reference to 'cout' is ambiguous

Right. std::cout and std::endl are found when lookup reaches the
global namespace (::). But since there is already your int-variable
with the same name (cout), you have the ambiguity.

The names are NOT pulled into ::main. That's not how a using directive
works. (see my previous post)

If you want cout to be found when lookup checks the function-local
scope, you need a using _declaration_ for that.

SG

unread,
Oct 30, 2013, 11:05:07 AM10/30/13
to
On Wednesday, October 30, 2013 3:56:27 PM UTC+1, SG wrote:
>
> Right. std::cout and std::endl are found when lookup reaches the
> global namespace (::). But since there is already your int-variable
> with the same name (cout), you have the ambiguity.
>
> The names are NOT pulled into ::main. That's not how a using directive
> works. (see my previous post)
>
> If you want cout to be found when lookup checks the function-local
> scope, you need a using _declaration_ for that.

Keep in mind that names are searched starting from local scopes and
going to the more global scopes. If/When one or more names are found
in the scope that namelookup is currently focussing on, this kind of
search stops and this is why names from inner scopes can "hide" names
from outer scopes.

In case the names found so far (if any) during unqualified lookup
refer to class members, namelookup will stop. Otherwise, "associated"
namespaces will be checked too (ADL, argument dependent lookup).

I think that covers all the nuts and bolts of unqualified name lookup.

HTH,
SG
0 new messages