 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthew Peltzer Guest
|
Posted: Tue Feb 17, 2004 11:40 am Post subject: namespace within a class |
|
|
Is this valid c++? If not, why not?
class a
{
namespace b { };
};
--Matthew Peltzer
[email]--goose23 (AT) spu (DOT) edu[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Feb 17, 2004 5:00 pm Post subject: Re: namespace within a class |
|
|
In message <75a1cd5a.0402161453.5f589fa3 (AT) posting (DOT) google.com>, Matthew
Peltzer <goose23 (AT) spu (DOT) edu> writes
| Quote: | Is this valid c++? If not, why not?
|
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)
| Quote: |
class a
{
namespace b { };
};
|
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.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tom Guest
|
Posted: Tue Feb 17, 2004 5:13 pm Post subject: Re: namespace within a class |
|
|
Matthew Peltzer wrote:
| Quote: | class a
{
namespace b { };
};
Is this valid c++?
|
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.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Richard Smith Guest
|
Posted: Tue Feb 17, 2004 5:17 pm Post subject: Re: namespace within a class |
|
|
[email]goose23 (AT) spu (DOT) edu[/email] (Matthew Peltzer) wrote in message news:<75a1cd5a.0402161453.5f589fa3 (AT) posting (DOT) google.com>...
| Quote: | Is this valid c++? If not, why not?
class a
{
namespace b { };
};
|
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?
--
Richard Smith
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Mang Guest
|
Posted: Tue Feb 17, 2004 6:29 pm Post subject: Re: namespace within a class |
|
|
Matthew Peltzer schrieb:
| Quote: | 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.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Paul Black Guest
|
Posted: Wed Feb 18, 2004 11:09 pm Post subject: Re: namespace within a class |
|
|
Tom wrote:
| Quote: | 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;
|
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:paul.black (AT) oxsemi (DOT) com
Oxford Semiconductor Ltd http://www.oxsemi.com
25 Milton Park, Abingdon, Tel: +44 (0) 1235 824 909
Oxfordshire. OX14 4SH Fax: +44 (0) 1235 821 141
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Feb 19, 2004 6:23 pm Post subject: Re: namespace within a class |
|
|
In message <c0vjf3$h59$1 (AT) jupiter (DOT) oxsemi.com>, Paul Black
<paul.black (AT) nospam (DOT) oxsemi.com> writes
| Quote: | Tom wrote:
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;
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. 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.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthew Peltzer Guest
|
Posted: Sat Feb 21, 2004 10:43 am Post subject: Re: namespace within a class |
|
|
Thomas Mang <a9804814 (AT) unet (DOT) univie.ac.at> wrote
| Quote: |
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.
| Quote: | In message <c0vjf3$h59$1 (AT) jupiter (DOT) oxsemi.com>, Paul Black
[email]paul.black (AT) nospam (DOT) oxsemi.com[/email]> writes
Tom wrote:
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.
| Quote: | 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.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| 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
|
|