|
|
Are using directives really transitive ?
$7.3.4/2: " If a scope contains a using-directive that nominates a
second namespace that itself contains using-directives, the effect is
as if the using-directives from the second namespace also appeared in
the first. "
Example given is:
namespace N {
int i;
}
namespace M {
int i;
using namespace N;
}
namespace L {
using namespace M;
}
void f() {
using namespace L;
i = 3; // ambiguity : M::i or N::i ?
}
int main() { f(); }
I guess, the ambiguity error occurs because the above code
(by the recursive application of the above rule) is translated to:
namespace N {
int i;
}
namespace M {
int i;
using namespace N;
}
namespace L {
using namespace M;
using namespace N; // "as if this appeared here also"
}
void f() {
using namespace L;
using namespace M; // "as if this appeared here also"
using namespace N; // "as if this appeared here also"
i = 3; // ambiguity : M::i or N::i ?
}
int main() { f(); }
Now consider what happens with 'qualified' names:
namespace N {
int i;
}
namespace M {
int i;
using namespace N; // Line1
}
namespace L {
using namespace M;
}
void f() {
using namespace L;
L::i = 3;
}
int main() { f(); }
NOTE: The above code compiles fine.
Because the name "L::i" is looked up in "L" and then in "M" (only).
As the 'using namespace N;' directive at Line1 is ignored becos M
contains a declaration of 'i'.
This is confirmed by the reference:
"For lookup of X::m, using-directives are ignored, in any namespace
directly containing one or
more declarations of m."
But according to the first reference cited, the above code should be
equivalent to:
namespace N {
int i;
}
namespace M {
int i;
using namespace N;
}
namespace L {
using namespace M;
using namespace N; // "as if this appeared here also"
}
void f() {
using namespace L;
using namespace M; // "as if this appeared here also"
using namespace N; // "as if this appeared here also"
L::i = 3; // Line1
}
int main() { f(); }
And this should compile fine as well (that is what 'equivalent' means,
right ?).
But the above fails to compile.
Any clues ?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
venkatesh
|
3/2/2009 8:10:24 PM |
|
On Mar 3, 7:10 am, venkatesh.p...@wipro.com wrote:
> $7.3.4/2: " If a scope contains a using-directive that nominates a
> second namespace that itself contains using-directives, the effect is
> as if the using-directives from the second namespace also appeared in
> the first. "
This is $7.3.4/4
> I guess, the ambiguity error occurs because the above code
> (by the recursive application of the above rule) is translated to:
>
> namespace N {
> int i;}
>
> namespace M {
> int i;
> using namespace N;}
>
> namespace L {
> using namespace M;
> using namespace N; // "as if this appeared here also"}
>
> void f() {
> using namespace L;
> using namespace M; // "as if this appeared here also"
> using namespace N; // "as if this appeared here also"
> i = 3; // ambiguity : M::i or N::i ?}
>
> int main() { f(); }
>
Take it a step further (now applying) $7.3.4/2. The effective code is
//namespace N {
int i;
//}
//namespace M {
int i;
// using namespace N;
//}
//namespace L {
// using namespace M;
//}
void f() {
// using namespace L;
i = 3; // ambiguity : M::i or N::i ?
}
int main() { f(); }
Dabs.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
rkldabs
|
3/4/2009 12:03:35 AM
|
|
On Mar 4, 11:03 am, rkld...@gmail.com wrote:
> On Mar 3, 7:10 am, venkatesh.p...@wipro.com wrote:
>
> > $7.3.4/2: " If a scope contains a using-directive that nominates a
> > second namespace that itself contains using-directives, the effect is
> > as if the using-directives from the second namespace also appeared in
> > the first. "
>
> This is $7.3.4/4
>
>
>
>
>
> > I guess, the ambiguity error occurs because the above code
> > (by the recursive application of the above rule) is translated to:
>
> > namespace N {
> > int i;}
>
> > namespace M {
> > int i;
> > using namespace N;}
>
> > namespace L {
> > using namespace M;
> > using namespace N; // "as if this appeared here also"}
>
> > void f() {
> > using namespace L;
> > using namespace M; // "as if this appeared here also"
> > using namespace N; // "as if this appeared here also"
> > i = 3; // ambiguity : M::i or N::i ?}
>
> > int main() { f(); }
>
> Take it a step further (now applying) $7.3.4/2. The effective code is
>
> //namespace N {
> int i;
> //}
>
> //namespace M {
> int i;
> // using namespace N;
> //}
>
> //namespace L {
> // using namespace M;
> //}
>
> void f() {
> // using namespace L;
> i = 3; // ambiguity : M::i or N::i ?
>
> }
>
> int main() { f(); }
{ edit: sig & banner removed. do not quote extraneous material. -mod }
I agree with what you have done for the unqualified name, 'i'.
But my question was 'How to apply that statement from reference (regd
transitivity) for the QUALIFIED name case ?"
May be the 'transitivity' statement should be 'qualified' (restricted)
a bit for the QUALIFIED name case ? :-)
Thanks in advance,
Venkat
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
venkat
|
3/4/2009 6:39:28 AM
|
|
|
2 Replies
138 Views
(page loaded in 0.036 seconds)
Similiar Articles:7/8/2012 10:05:21 PM
|
|
|
|
|
|
|
|
|