C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

namespace within a class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthew Peltzer
Guest





PostPosted: Tue Feb 17, 2004 11:40 am    Post subject: namespace within a class Reply with quote



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





PostPosted: Tue Feb 17, 2004 5:00 pm    Post subject: Re: namespace within a class Reply with quote



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





PostPosted: Tue Feb 17, 2004 5:13 pm    Post subject: Re: namespace within a class Reply with quote



Matthew Peltzer wrote:

Quote:
class a
{
namespace b { };
};

Is this valid c++?

No.

Quote:
If not, why not?

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





PostPosted: Tue Feb 17, 2004 5:17 pm    Post subject: Re: namespace within a class Reply with quote

[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





PostPosted: Tue Feb 17, 2004 6:29 pm    Post subject: Re: namespace within a class Reply with quote



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





PostPosted: Wed Feb 18, 2004 11:09 pm    Post subject: Re: namespace within a class Reply with quote

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





PostPosted: Thu Feb 19, 2004 6:23 pm    Post subject: Re: namespace within a class Reply with quote

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





PostPosted: Sat Feb 21, 2004 10:43 am    Post subject: Re: namespace within a class Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.