Old Wolf wrote in news:843a4f78.0411231359.5abc33bb@posting.google.c om in
comp.lang.c++:
[color=blue]
> Rob Williscroft <rtw@freenet.co.uk> wrote:[color=green]
>> Petter Reinholdtsen wrote:
>>[color=darkred]
>> > Is this code legal ANSI C++, if it is in a file on its own?
>> >
>> > using namespace std;[/color]
>>
>> BTW don't expect the standard to say the likes off:
>> " ... the identifier *must* be an existing namespace-name ... "
>> as it doesen't need too, as it is specified in the grammar.[/color]
>
> using-directive:
> using namespace ::(opt) nested-name-specifier(opt) namespace-name ;
>
> namespace-name:
> original-namespace-name
> namespace-alias
>
> namespace-alias:
> identifier
>
> original-namespace-name:
> identifier
>
> and "std" fits the defintion of "identifier", so it seems to be
> valid according to the grammar.
>[/color]
My apologies, I gave the impression that *all* you had to do
was read the grammar, which is clearly incorrect.
After reading 7.3.1/1 (the grammar) follow it with /2:
The identifier in an original-namespace-definition shall not have
been previously defined in the declarative region in which the
original-namespace-definition appears. The identifier in an
original-namespace definition is the name of the namespace.
Subsequently in that declarative region, it is treated as an
original-namespace-name.
So an identifier *isn't* an original-namespace-name until
it has appeared in an original-namespace-definition.
The point I was trying (failing) to make was that you need to
read the grammar, you can't just rely on the normal text.
The grammar connects namespace-name and original-namespace-name.
Further reading connects original-namespace-definition and the
proscription that an original-namespace-name only exists *after*
the original-namespace-defenition.
[color=blue]
> AFAICS the relevant section is 3.4.6:
> When looking up a /namespace-name/ in a /using-directive/
> or /namespace-alias-definition/, only namespace names are
> considered.[/color]
That is saying other names are not considered:
#include <iostream>
namespace A
{
namespace B { int x; }
}
class B {};
int B;
using namespace A;
using namespace B;
int main()
{
x = 10;
std::cout << x << " Ok\n";
}
(* Note: gcc (g++) doesn't get this right *)
[color=blue]
>
> If 'std' is not a namespace name at the point of lookup (which
> I presume is the point of the using directive), then the lookup
> must fail (which I again presume means that there should be a
> compiler error).[/color]
Rob.
--
http://www.victim-prime.dsl.pipex.com/