Namespace in class?

V

Victor Bazarov

Why can’t I put “using namespace name” inside class body?

Because the Standard prohibits it (in [namespace.udir]). Can you tell
us what might be the purpose? What problem would you solve with that?

V
 
N

Nephi Immortal

   Why can’t I put “using namespace name” inside class body?

Because the Standard prohibits it (in [namespace.udir]).  Can you tell
us what might be the purpose?  What problem would you solve with that?

Well…. It is what I expect….

class name {
using namespace name2;

func1();
func2();
};

instead of …

class name {
name2::func1();
name2::func2();
};

It is much easier rather than placing using namespace name2 inside
each member function’s body or global scope. I hate to add namespace
before member function declaration each time like name2::func1(),
name2::func2(), …..
 
R

Rolf Magnus

Nephi said:
Why can’t I put “using namespace name†inside class body?

Because the Standard prohibits it (in [namespace.udir]). Can you tell
us what might be the purpose? What problem would you solve with that?

Well…. It is what I expect….

class name {
using namespace name2;

func1();
func2();
};

instead of …

class name {
name2::func1();
name2::func2();
};

Huh? Are those functions suposed to be member functions of the class or non-
member functions in namespace name2? They can't be both at the same time.
 
Ö

Öö Tiib

On 2/10/2011 2:34 PM, Nephi Immortal wrote:
Because the Standard prohibits it (in [namespace.udir]).  Can you tell
us what might be the purpose?  What problem would you solve with that?

        Well….  It is what I expect….

class name {
        using namespace name2;

        func1();
        func2();

};

What are these "func1()" and "func2()"? By syntax and location they
look like constructors but class is name so constructor should be
"name()"? If these are member function declarations then they lack
type. C++ does not allow implicit type.

instead of …

class name {
        name2::func1();
        name2::func2();

};

That is totally confusing what you try to do here. You can not put
only part of your class (some members or constructors) into namespace
with such a syntax. You must put whole class. If you want to put your
"class name" into "namespace name2" then you should write:

namespace name2
{
class name
{
public:
name();
int func1();
};
}
        It is much easier rather than placing using namespace name2 inside
each member function’s body or global scope.  I hate to add namespace
before member function declaration each time like name2::func1(),
name2::func2(), …..

Yes, but C++ does not let to put namespaces (or even class name)
before member function name in member function declarations anyway. So
you seemingly fight with something that is not even allowed?
 
S

Stuart Golodetz

Why can’t I put “using namespace name” inside class body?

Because the Standard prohibits it (in [namespace.udir]). Can you tell
us what might be the purpose? What problem would you solve with that?

Well…. It is what I expect….

class name {
using namespace name2;

func1();
func2();
};

instead of …

class name {
name2::func1();
name2::func2();
};

It is much easier rather than placing using namespace name2 inside
each member function’s body or global scope. I hate to add namespace
before member function declaration each time like name2::func1(),
name2::func2(), …..

You can simulate what you want by doing this:

###
namespace RandomName {

using namespace NS;

class C
{
//using namespace NS; // illegal
};

}

using RandomName::C;
###

It's actually useful some of the time, too - particularly when you want
to avoid writing e.g. boost:: a lot in a header (anyone who's familiar
with Boost.Multi-Index may have been annoyed by this sort of thing). You
definitely don't want to writing using namespace boost; in the global
namespace in a header, but you also don't want to write boost:: in front
of everything. A using directive scoped to the class would be ideal, but
it's not allowed.

HTH,
Stu
 
S

Stuart Golodetz

On 2/10/2011 2:34 PM, Nephi Immortal wrote:

Why can’t I put “using namespace name” inside class body?

Because the Standard prohibits it (in [namespace.udir]). Can you tell
us what might be the purpose? What problem would you solve with that?

Well…. It is what I expect….

class name {
using namespace name2;

func1();
func2();
};

instead of …

class name {
name2::func1();
name2::func2();
};

It is much easier rather than placing using namespace name2 inside
each member function’s body or global scope. I hate to add namespace
before member function declaration each time like name2::func1(),
name2::func2(), …..

You can simulate what you want by doing this:

###
namespace RandomName {

using namespace NS;

class C
{
//using namespace NS; // illegal
};

}

using RandomName::C;
###

It's actually useful some of the time, too - particularly when you want
to avoid writing e.g. boost:: a lot in a header (anyone who's familiar
with Boost.Multi-Index may have been annoyed by this sort of thing). You
definitely don't want to writing using namespace boost; in the global
namespace in a header, but you also don't want to write boost:: in front
of everything. A using directive scoped to the class would be ideal, but
it's not allowed.

HTH,
Stu

As a real example, I have a class template in my most recent project
that looks like the below. It would be substantially more annoying to
write the EdgeContainer typedef without the using directive (well, I
think so anyway).

Cheers,
Stu

###
namespace mp {

namespace mp_AdjacencyGraph {

using namespace boost::multi_index;

template <typename NodeProperties, typename EdgeWeight>
class AdjacencyGraph
{
public:
typedef WeightedEdge<EdgeWeight> Edge;

private:
// Tags
struct tagDefault;
struct tagSmaller;
struct tagLarger;

typedef multi_index_container<
Edge,
indexed_by<
ordered_unique<tag<tagDefault>,
composite_key<
Edge,
member<Edge,int,&Edge::u>,
member<Edge,int,&Edge::v>
>
>,
ordered_non_unique<tag<tagSmaller>,
member<Edge,int,&Edge::u>
>,
ordered_non_unique<tag<tagLarger>,
member<Edge,int,&Edge::v>
>
>
> EdgeContainer;

// etc.
};

}

using mp_AdjacencyGraph::AdjacencyGraph;

}
###
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top