PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Valid C++?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Valid C++?

Réponse
 
LinkBack Outils de la discussion
Vieux 17/10/2007, 11h15   #1
Boris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Valid C++?

Is this a valid C++ program (according to the C++ standard)? I ask as I'm
trying to port a Windows library to Solaris, and the Solaris C++ compiler
reports an error (The name x is ambiguous in ns1: ns2::x and ns1::x). The
code below reproduces the problem, and I wonder now if I have to adapt the
code or submit a bug report to Sun?

namespace ns1 {
struct x { };
}

namespace ns2 {
using namespace ns1; // If this line is removed ...
void x(); // ... or this line the code compiles.
struct x { };
}

int main()
{
struct ns2::x x;
}
  Réponse avec citation
Vieux 17/10/2007, 12h05   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Valid C++?

* Boris:
> Is this a valid C++ program (according to the C++ standard)? I ask as
> I'm trying to port a Windows library to Solaris, and the Solaris C++
> compiler reports an error (The name x is ambiguous in ns1: ns2::x and
> ns1::x). The code below reproduces the problem, and I wonder now if I
> have to adapt the code or submit a bug report to Sun?
>
> namespace ns1 {
> struct x { };
> }
>
> namespace ns2 {
> using namespace ns1; // If this line is removed ...
> void x(); // ... or this line the code compiles.
> struct x { };
> }
>
> int main()
> {
> struct ns2::x x;
> }


As I recall the code is formally valid but dumb.

Since it's dumb I won't make the effort to look up the standard's
guarantees for you.

Rename.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 17/10/2007, 12h43   #3
Jonathan Lane
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Valid C++?

On Oct 17, 11:15 am, Boris <bo...@gtemail.net> wrote:
> Is this a valid C++ program (according to the C++ standard)? I ask as I'm
> trying to port a Windows library to Solaris, and the Solaris C++ compiler
> reports an error (The name x is ambiguous in ns1: ns2::x and ns1::x). The
> code below reproduces the problem, and I wonder now if I have to adapt the
> code or submit a bug report to Sun?
>
> namespace ns1 {
> struct x { };
>
> }
>
> namespace ns2 {
> using namespace ns1; // If this line is removed ...
> void x(); // ... or this line the code compiles.
> struct x { };
>
> }
>
> int main()
> {
> struct ns2::x x;
>
> }


Clearly the "using namespace ns1" line is going to be a problem since
you're polluting your ns2 namespace with the colliding names from ns1.
I can understand that this is going to make ns2 contain ambiguous
names.

Solutions:
rename your functions and structs so that they're unique. This also
makes the maintenance a lot easier since a developer doesn't need to
think about whether you mean function x, struct x, or alternative
struct x when they read your code.

and/or don't import the whole of ns1 into ns2. Use the fully qualified
namespace name to address ns1::x. namespaces are there to prevent this
kind of collision. By importing the whole namespace you essentially
bypass that benefit.

  Réponse avec citation
Vieux 17/10/2007, 13h12   #4
Boris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Valid C++?

On Wed, 17 Oct 2007 14:43:50 +0300, Jonathan Lane
<jonathan.lane1@googlemail.com> wrote:

> On Oct 17, 11:15 am, Boris <bo...@gtemail.net> wrote:
>> Is this a valid C++ program (according to the C++ standard)? I ask as
>> I'm
>> trying to port a Windows library to Solaris, and the Solaris
>> C++ compiler
>> reports an error (The name x is ambiguous in ns1: ns2::x and ns1::x).
>> The
>> code below reproduces the problem, and I wonder now if I have to adapt
>> the
>> code or submit a bug report to Sun?
>>
>> namespace ns1 {
>> struct x { };
>>
>> }
>>
>> namespace ns2 {
>> using namespace ns1; // If this line is removed ...
>> void x(); // ... or this line the code compiles.
>> struct x { };
>>
>> }
>>
>> int main()
>> {
>> struct ns2::x x;
>>
>> }

>
> Clearly the "using namespace ns1" line is going to be a problem since
> you're polluting your ns2 namespace with the colliding names from ns1.
> I can understand that this is going to make ns2 contain ambiguous
> names.


Normally I'd agree. However I you remove the function declaration the code
compiles although the namespace is still polluted?

> Solutions:
> rename your functions and structs so that they're unique. This also
> makes the maintenance a lot easier since a developer doesn't need to
> think about whether you mean function x, struct x, or alternative
> struct x when they read your code.


Renaming the structs would be the last resort as from a design point of
view the names used in the library make sense and represent two different
concepts which happen to have the same name. The function is also
intentionally called like the struct: The struct is a function object, and
other parts of the code might want to reuse the function without
instantiating the function object first. I have no strict opinion here as
you can come up with other conventions of course. However I shouldn't need
to if this is valid C++ code?

Boris
  Réponse avec citation
Vieux 17/10/2007, 13h37   #5
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Valid C++?

Alf P. Steinbach wrote:
> * Boris:
>> Is this a valid C++ program (according to the C++ standard)? I ask as
>> I'm trying to port a Windows library to Solaris, and the Solaris C++
>> compiler reports an error (The name x is ambiguous in ns1: ns2::x and
>> ns1::x). The code below reproduces the problem, and I wonder now if I
>> have to adapt the code or submit a bug report to Sun?
>>
>> namespace ns1 {
>> struct x { };
>> }
>>
>> namespace ns2 {
>> using namespace ns1; // If this line is removed ...
>> void x(); // ... or this line the code compiles.
>> struct x { };
>> }
>>
>> int main()
>> {
>> struct ns2::x x;
>> }

>
> As I recall the code is formally valid but dumb.
>
> Since it's dumb I won't make the effort to look up the standard's
> guarantees for you.
>
> Rename.


Or remove the 'using' from 'ns2'. It really is A BAD IDEA(tm) to
have a 'using' directive in the header (no matter whether it's in
another namespace or not).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 17/10/2007, 17h00   #6
Jonathan Lane
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Valid C++?

On Oct 17, 1:12 pm, Boris <bo...@gtemail.net> wrote:
> On Wed, 17 Oct 2007 14:43:50 +0300, Jonathan Lane
>
>
>
> <jonathan.la...@googlemail.com> wrote:
> > On Oct 17, 11:15 am, Boris <bo...@gtemail.net> wrote:
> >> Is this a valid C++ program (according to the C++ standard)? I ask as
> >> I'm
> >> trying to port a Windows library to Solaris, and the Solaris
> >> C++ compiler
> >> reports an error (The name x is ambiguous in ns1: ns2::x and ns1::x).
> >> The
> >> code below reproduces the problem, and I wonder now if I have to adapt
> >> the
> >> code or submit a bug report to Sun?

>
> >> namespace ns1 {
> >> struct x { };

>
> >> }

>
> >> namespace ns2 {
> >> using namespace ns1; // If this line is removed ...
> >> void x(); // ... or this line the code compiles.
> >> struct x { };

>
> >> }

>
> >> int main()
> >> {
> >> struct ns2::x x;

>
> >> }

>
> > Clearly the "using namespace ns1" line is going to be a problem since
> > you're polluting your ns2 namespace with the colliding names from ns1.
> > I can understand that this is going to make ns2 contain ambiguous
> > names.

>
> Normally I'd agree. However I you remove the function declaration the code
> compiles although the namespace is still polluted?


presumably then the ns2 version of the struct is hiding the ns1
version. How would you expect the compiler/your code to differentiate
between the two structs given that they're both in scope? There's
nothing to stop you having a function and struct/functor of the same
name. I'd suggest it's a bit misleading to maintenance programmers but
that's up to you of course. I guess the compiler's getting confused by
having three conflicting names. I'm sure there's a scoping rule that
affects this.

If you won't rename the structs then at least get rid of the using
directive in ns2. There's no need for it at all in this case and it's
just causing you problems.


  Réponse avec citation
Vieux 17/10/2007, 17h10   #7
Boris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Valid C++?

On Wed, 17 Oct 2007 19:00:08 +0300, Jonathan Lane
<jonathan.lane1@googlemail.com> wrote:

> [...]
>> > Clearly the "using namespace ns1" line is going to be a problem since
>> > you're polluting your ns2 namespace with the colliding names from ns1.
>> > I can understand that this is going to make ns2 contain ambiguous
>> > names.

>>
>> Normally I'd agree. However I you remove the function declaration the
>> code
>> compiles although the namespace is still polluted?

>
> presumably then the ns2 version of the struct is hiding the ns1
> version. How would you expect the compiler/your code to differentiate
> between the two structs given that they're both in scope? There's
> nothing to stop you having a function and struct/functor of the same
> name. I'd suggest it's a bit misleading to maintenance programmers but
> that's up to you of course. I guess the compiler's getting confused by
> having three conflicting names. I'm sure there's a scoping rule that
> affects this.
>
> If you won't rename the structs then at least get rid of the using
> directive in ns2. There's no need for it at all in this case and it's
> just causing you problems.


Yep, that's what I'll do. Thanks for your comments!

Boris
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h48.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13847 seconds with 15 queries