You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@qpid.apache.org by Steve Huston <sh...@riverace.com> on 2008/09/10 18:13:01 UTC

C++ namespaces and 'using' question

Hi,

In working on the Windows port, I've come across a few places where
there's C++ using-directives such as:

using namespace qpid::framing;

...

int MyClass::Method (framing::Buffer &b)
...

g++ is ok with this construct, but Microsoft Visual C++ complains that
framing is unknown. Essentially, what VC wants is either:

using qpid::framing;

Or

using namespace qpid;

With code as is today, apparantly VC is trying to find "framing"
within "qpid::framing", but not use "qpid::framing" itself.

I don't know C++ language and verse well enough to say what's right
with any authority, but I believe MSVC is right here (and I think the
Solaris compiler did the same thing). So far this sort of thing has
been fixed by prepending qpid:: to the method signature (in the
example above).

I'd like to come to a concensus on what to do in this type of case,
and would propose that we use a using-directive for one namespace
higher than where the "most" different namespaces are used. For
example, if code often refers to qpid::framing and qpid::broker and
qpid::sys, then a single 

using namespace qpid;

Would allow code to refer to framing::name, broker::name, and
sys::name, which is what I think the original intent was.

How does this sound? I can put a jira for this as well, if it merits
further discussion, but I don't want to get into a code format war.

Thanks,
-Steve



RE: C++ namespaces and 'using' question

Posted by Steve Huston <sh...@riverace.com>.
Hi Danushka,

> > To be pedantic if you have "using qpid::framing" then this tells
the
> > compier that any unqualified reference to "framing" is actually a
> > reference to "qpid::framing" so you can't remove the "framing::"
> > prefixes. ["using qpid::framing" == namespace framing = 
> qpid::framing"
> > in this context]
> >
> >   
> Andrew,
> 
> I dont think you can have "using qpid::framing" but "using
/namespace 
> /qpid::framing" as qpid::framing is not a symbol but a namespace.

Right.

After working a bit more on this, this is what I'd like to do:

- Not put in a blanket "using namespace qpid" - I agree that's too
far.

- Use, for example, "namespace framing = qpid::framing" as Andrew
mentions above. Then the existing framing::... works on both g++ and
MSVC.

Also, a guideline I'd like to see, and would like to hear more from
you all, is to always have some sort of namespace tag on classes that
are outside the namespace being worked on. For example, I was looking
at qpid/management/ManagementBroker.cpp - it refers to classes from
framing and broker as well. I'd like those to be explictly referred
to, such as:

namespace broker = qpid::broker;
namespace framing = qpid::framing;

...

framing::Buffer  buff;
...

Otherwise newbies like me have no clue where all these classes are
coming from. With the namespaces I have a good starting point for
where to look for more info on the class in question.

-Steve



Re: C++ namespaces and 'using' question

Posted by Danushka Menikkumbura <da...@wso2.com>.
> To be pedantic if you have "using qpid::framing" then this tells the
> compier that any unqualified reference to "framing" is actually a
> reference to "qpid::framing" so you can't remove the "framing::"
> prefixes. ["using qpid::framing" == namespace framing = qpid::framing"
> in this context]
>
>   
Andrew,

I dont think you can have "using qpid::framing" but "using /namespace 
/qpid::framing" as qpid::framing is not a symbol but a namespace.

Danushka

-- 
Danushka Menikkumbura
Technical Lead, WSO2 Inc.

blog : http://danushka-menikkumbura.blogspot.com/

http://wso2.com/ - "The Open Source SOA Company"



Re: C++ namespaces and 'using' question

Posted by Andrew Stitcher <as...@redhat.com>.
On Thu, 2008-09-11 at 08:20 -0400, Alan Conway wrote:
> ... 2. Removing all un-necessary namespace prefixes - if you have using
> namespace qpid::framing then you can just remove all the framing::
> prefixes.
> 

To be pedantic if you have "using qpid::framing" then this tells the
compier that any unqualified reference to "framing" is actually a
reference to "qpid::framing" so you can't remove the "framing::"
prefixes. ["using qpid::framing" == namespace framing = qpid::framing"
in this context]

Andrew



Re: C++ namespaces and 'using' question

Posted by Alan Conway <ac...@redhat.com>.
On Wed, 2008-09-10 at 20:45 -0400, Carl Trieloff wrote:
> Steve Huston wrote:
> > Hi,
> >
> > In working on the Windows port, I've come across a few places where
> > there's C++ using-directives such as:
> >
> > using namespace qpid::framing;
> >
> > ...
> >
> > int MyClass::Method (framing::Buffer &b)
> > ...
> >
> > g++ is ok with this construct, but Microsoft Visual C++ complains that
> > framing is unknown. Essentially, what VC wants is either:

g++ would not be ok with that unless you also have a 

using namespace qpid;

or MyClass is defined inside the qpid namespace.

I suspect the issue is that when you have:

namespace qpid { namespace broker { framing::Foo x; }}

gcc is checking all the surrounding namespaces (qpid::broker:: and
qpid::) but MSVC is checking only the exact current namespace
(qpid::broker::)

Not sure which is correct.

> >
> > using qpid::framing;
> >
> > Or
> >
> > using namespace qpid;
> 
> I think this one is the way to go and the correct per c++  -- andrew y/n?
> 
> using namespace qpid;
> 
> But we should make sure it works across all the complier options we support.
> 
> Carl.

That's correct but a bit draconian. I suspect the issue you describe can
be resolved by either:
 1. adding an extra using namespace qpid; which is easy.
 2. Removing all un-necessary namespace prefixes - if you have using
namespace qpid::framing then you can just remove all the framing::
prefixes.

I also don't want to get into a format war, but before we propose a rule
that causes a reformat of the entire codebase I'd like to be sure we
have identified the least painful fix. Adding extra using statements or
removing redundant prefixes is much easier than adding prefixes to every
un-prefixed identifier in every file that has such a using statement.

Cheers,
Alan.


Re: C++ namespaces and 'using' question

Posted by Carl Trieloff <cc...@redhat.com>.
Steve Huston wrote:
> Hi,
>
> In working on the Windows port, I've come across a few places where
> there's C++ using-directives such as:
>
> using namespace qpid::framing;
>
> ...
>
> int MyClass::Method (framing::Buffer &b)
> ...
>
> g++ is ok with this construct, but Microsoft Visual C++ complains that
> framing is unknown. Essentially, what VC wants is either:
>
> using qpid::framing;
>
> Or
>
> using namespace qpid;

I think this one is the way to go and the correct per c++  -- andrew y/n?

using namespace qpid;

But we should make sure it works across all the complier options we support.

Carl.



Re: C++ namespaces and 'using' question

Posted by Andrew Stitcher <as...@redhat.com>.
On Wed, 2008-09-10 at 12:13 -0400, Steve Huston wrote:
> Hi,
> 
> In working on the Windows port, I've come across a few places where
> there's C++ using-directives such as:
> 
> using namespace qpid::framing;

I believe the intent of this code is "using qpid::framing", and the code
author got a little confused!

> 
> ...
> 
> int MyClass::Method (framing::Buffer &b)
> ...
> 
> g++ is ok with this construct, but Microsoft Visual C++ complains that
> framing is unknown. Essentially, what VC wants is either:
> 
> using qpid::framing;
> 
> Or
> 
> using namespace qpid;
> 
> With code as is today, apparantly VC is trying to find "framing"
> within "qpid::framing", but not use "qpid::framing" itself.
> 
> I don't know C++ language and verse well enough to say what's right
> with any authority, but I believe MSVC is right here (and I think the
> Solaris compiler did the same thing). So far this sort of thing has
> been fixed by prepending qpid:: to the method signature (in the
> example above).

I'm not 100%, but I think you're correct that g++ is too lenient here -
the best place to test this is on the dinkumware website using the edg
compiler.

> ...
> How does this sound? I can put a jira for this as well, if it merits
> further discussion, but I don't want to get into a code format war.

On the whole I don't feel all that strongly about it, but I tend to
think that being more explicit is better so I'd prefer to see:

using qpid::framing;
using qpid::sys;
etc.

in the files that merit it, rather than a blanket:
using namespace qpid;

Andrew