namespace within a class

namespace within a class

Post by goose2 » Wed, 18 Feb 2004 20:40:40


Is this valid c++? If not, why not?

class a
{
namespace b { };
};



--Matthew Peltzer
XXXX@XXXXX.COM

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

namespace within a class

Post by Francis Gl » Thu, 19 Feb 2004 02:00:11

In message < XXXX@XXXXX.COM >, Matthew
Peltzer < XXXX@XXXXX.COM > writes

No. Because WG21 did not choose to allow it. There reasons for doing so
include that it would introduce even more complexity to an already very
fragile area of C++ (specifying name-lookup rules)


If you really need such a form just use a monostate class. That avoids
introducing extra complexity to the language because we do not have to
consider such things as using declarations/directives for namespaces at
class scope.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.yqcomputer.com/
For project ideas and contributions: http://www.yqcomputer.com/


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
 
 

namespace within a class

Post by Thomas8675 » Thu, 19 Feb 2004 02:13:46


No.


Why should it be? The standard does not permit it. It also wouldn't
make any sense. When something is at "namespace scope", it is by
definition outside of any class or function. All declarations within
the class definition, on the other hand, are already qualified with
the class name, so what would it mean for them to be part of a
namespace? If you need to do the equivalent of partitioning the
namespace within a class for some reason, you can get that
functionality with an internal class:

class A {
public:
class B {
public:
static int i;
};
static int i;
};

int A::i;
int A::B::i;

Your code is invalid for a second reason as well: a semicolon should
not follow the closing bracket at the end of a namespace definition.

Best regards,

Tom

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

namespace within a class

Post by richar » Thu, 19 Feb 2004 02:17:40


No, it is not valid C++.

As to why it isn't valid, well, why should it be valid? What do you
want it to mean? How (and why) would you want to use it? And what
would it offer that can't cleanly be achieved in the existing
language?

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

namespace within a class

Post by Thomas Man » Thu, 19 Feb 2004 03:29:25


Matthew Peltzer schrieb:

> Is this valid c++? If not, why not?
>
> class a
> {
> namespace b { };
> };

It is illegal by 7.3.1. / 4:

"Every namespace-definition shall appear in the global scope or in a
namespace sope".

If you want to further divide the scope of a, you can always use a
nested struct/class.


regards,

Thomas



[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

namespace within a class

Post by Paul Blac » Fri, 20 Feb 2004 08:09:50


One thing that a namespace in a class can do which your wokaround can
not is to allow the use of "using".

--
Paul Black mailto: XXXX@XXXXX.COM
Oxford Semiconductor Ltd http://www.yqcomputer.com/
25 Milton Park, Abingdon, Tel: +44 (0) 1235 824 909
Oxfordshire. OX14 4SH Fax: +44 (0) 1235 821 141

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

namespace within a class

Post by Francis Gl » Sat, 21 Feb 2004 03:23:07

In message <c0vjf3$h59$ XXXX@XXXXX.COM >, Paul Black
< XXXX@XXXXX.COM > writes


And that is one of the reasons that we did not provide for class scope
namespaces. Trying to work out the semantics of using declarations and
directives in such circumstances is a nightmare that we preferred to
avoid.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.yqcomputer.com/
For project ideas and contributions: http://www.yqcomputer.com/


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

namespace within a class

Post by goose2 » Sun, 22 Feb 2004 19:43:51


> >
> > class a
> > {
> > namespace b { };
> > };
>
> It is illegal by 7.3.1. / 4:
>
> "Every namespace-definition shall appear in the global scope or in a
> namespace sope".

That's the answer to my first question I was looking for, thanks.

> In message <c0vjf3$h59$ XXXX@XXXXX.COM >, Paul Black
> < XXXX@XXXXX.COM > writes

> >One thing that a namespace in a class can do which your wokaround can
> >not is to allow the use of "using".
>
> And that is one of the reasons that we did not provide for class scope
> namespaces.

That's a good answer to my second question, thanks.

> Trying to work out the semantics of using declarations and
> directives in such circumstances is a nightmare that we preferred to
> avoid.

Right. What I settled on was something similar to:

class a
{
class b { };
static b _b;
};

At first all I really wanted for a couple of functions within my class
to be restricted from some members of the class. Some how I thought a
namesapce would make sense, and when the compiler barfed, I looked at
the standard but did not find anything there, hence my post. Once I
realized I was going to need some objects, turning the namespace into
a class was the obvious solution.

Again, thank you all for your insights.

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]