 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Keith H Duggar Guest
|
Posted: Tue Sep 19, 2006 4:45 am Post subject: qualified names in namespace member declarations |
|
|
There is some previous discussion of this; but, I could not
find definitive and unanimous agreement and interpretation
of the standard.
namespace A {
class A::SomeClass { } ;
A::SomeClass A::some_func() ;
}
namespace A {
A::SomeClass A::some_func() { return A::SomeClass() ; }
}
Which of the redundant A:: qualifications above are legal?
Is section 8.3 of the standard relevant? What other sections
bear on this question?
On our system, g++ and the Sun compiler allow the above while
the Intel compiler gives the "qualified name not allowed in
namespace member declaration" error discussed here a few
years back.
Finally, is it legal to define some_func without wrapping in
namespace A { } as one does for class member functions?
A::SomeClass A::some_func() { return A::SomeClass() ; }
Thanks in advance!
-- Keith -- Fraud 6
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Tue Sep 19, 2006 3:29 pm Post subject: Re: qualified names in namespace member declarations |
|
|
Keith H Duggar wrote:
| Quote: | There is some previous discussion of this; but, I could not
find definitive and unanimous agreement and interpretation
of the standard.
namespace A {
class A::SomeClass { } ;
A::SomeClass A::some_func() ;
}
namespace A {
A::SomeClass A::some_func() { return A::SomeClass() ; }
}
Which of the redundant A:: qualifications above are legal?
Is section 8.3 of the standard relevant? What other sections
bear on this question?
|
Neither of the "A::" qualifications are redundant; and both - being
unmatched - are illegal. §8.3 is relevant because it states that the
qualified name of a namespace can be used to refer that that namespace
only from outside of the named namespace. So whichever namespace (or
class) "A" is meant to refer to - it cannot be the enclosing A
namespace. And since no other namespace or class named "A" are anywhere
in evidence, the program is ill-formed.
| Quote: | On our system, g++ and the Sun compiler allow the above while
the Intel compiler gives the "qualified name not allowed in
namespace member declaration" error discussed here a few
years back.
|
I am happy to report that the gcc 4.01 compiler rejects these
declarations with the same error as the Intel compiler. So it appears
that the steady march of C++ compilers toward full compliance,
continues.
| Quote: | Finally, is it legal to define some_func without wrapping in
namespace A { } as one does for class member functions?
A::SomeClass A::some_func() { return A::SomeClass() ; }
|
Consider this "corrected" version of the program above:
namespace A
{
namespace A
{
class SomeClass;
SomeClass some_func();
}
class A::SomeClass{ };
A::SomeClass A::some_func()
{
return A::SomeClass();
}
}
This program is well-formed because the namespace "A" (along with its
"SomeClass" class and "some_func" function declarations) is present.
So the answer is "yes" - it is legal to define a function, class or
variable outside of its namespace (by providing the appropriate
qualifications.) But, as shown in the above example, there must always
be a matching, unqualified declaration within the namespace in order
for the external definition to be legal.
Greg
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Aleksey Loginov Guest
|
Posted: Thu Sep 21, 2006 3:38 pm Post subject: Re: qualified names in namespace member declarations |
|
|
Greg Herlihy wrote:
| Quote: |
I am happy to report that the gcc 4.01 compiler rejects these
declarations with the same error as the Intel compiler. So it appears
that the steady march of C++ compilers toward full compliance,
continues.
|
what about
namespace A {
class SomeClass { } ;
A::SomeClass some_func() ;
}
namespace A {
A::SomeClass some_func() { return A::SomeClass(); }
}
Comeau compile it.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sun Sep 24, 2006 6:55 pm Post subject: Re: qualified names in namespace member declarations |
|
|
Aleksey Loginov wrote:
| Quote: | what about
namespace A {
class SomeClass { } ;
A::SomeClass some_func() ;
}
namespace A {
A::SomeClass some_func() { return A::SomeClass(); }
}
Comeau compile it.
|
Good point. I should have limited my example to declarations and
function names - since other names, such as type names and variable
names can be namespace qualified anywhere. In fact, my answer should
have been to wait for C++0x. It appears that the Committee has decided
to drop 8.3's restriction and allow qualified names within their own
namespace.
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#482
So from what I can tell, the original code will then be legal in the
future.
And as an editorial aside: I don't really see much benefit from this
decision. It seems to grant names in C++ an even greater potential for
ambiguity - and does so just for the convenience of the programmer
being able to type a longer name.
Greg
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Manfred von Willich Guest
|
Posted: Thu Sep 28, 2006 8:48 pm Post subject: Re: qualified names in namespace member declarations |
|
|
Greg Herlihy wrote:
| Quote: | And as an editorial aside: I don't really see much benefit from this
decision. It seems to grant names in C++ an even greater potential for
ambiguity - and does so just for the convenience of the programmer
being able to type a longer name.
|
Despite being a relative outsider, it is difficult for me to refrain
from adding my two cents' worth to this one.
Con:
* It is not clear to me whether any existing code will break - in
particular, code in which class or namespace is declared inside a
namespace of the same name. Name lookup from the innermost namespace
to the outermost may resolve this ambiguity correctly in all cases -
something the experts have probably looked into.
Pro:
* The rule for disambiguating any name qualification name should become
uniform for all contexts. Most importantly, the rule will be intuitive
and easily remembered.
* The change would bring the treatment of qualified names for
namespaces and classes closer together. Ideally, I would like to see
class declarations being defined as generating a namespace with the
same name, and that all name qualification refers to namespace names
only.
I remember a discussion in this group relating to injected class names
that may make the latter point less applicable, though I am unsure of
this. I would prefer "injected" to mean that a class (or namespace)
name used as the first component in a qualified name is visible by
virtue of searching outwards through nested namespaces/classes, rather
than that it is in effect redeclared as nested inside its own scope.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|