Inject std namespace into main()

P

parmenides

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()?
 
A

Alf P. Steinbach

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()?

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;
}

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
 
S

SG

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
 
S

SG

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

SG

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top