namespace

A

Agent Mulder

I have problems moving my project
into a seperate namespace.
Namespace introduces its own level
of bugs.

#include <iostream.h>
class Fury{};//comment this out and it compiles
namespace Green
{
class Fury{};//or comment this out and it compiles
};
using namespace Green;//or comment this out and it compiles
int main(int argc,char**argv)
{
Fury fury;//or comment this out and it compiles
return 0;
}

-X
 
A

Alex Hirner

Agent Mulder said:
I have problems moving my project
into a seperate namespace.
Namespace introduces its own level
of bugs.

#include <iostream.h>
class Fury{};//comment this out and it compiles
namespace Green
{
class Fury{};//or comment this out and it compiles
};
using namespace Green;//or comment this out and it compiles
int main(int argc,char**argv)
{
Fury fury;//or comment this out and it compiles
return 0;
}

-X

let's think about.
When you do not comment anything out you have a
::Fury and a Green::Fury.

so far so good. But then you concatenate the Green-namespace to the global
one and the two collide.
(f.e. the compile does'nt know which one to choose)
Isn't that obvious? So you actually defeat the advantage to use equally
named symbols in different
namespaces by merging the seperate namespace to the global one.

Here's a how I would use it:

#include <iostream.h>
class Fury{};
namespace Green
{
class Fury{};
};
// using namespace Green;//or comment this out and it compiles, yeah do it!
int main(int argc,char**argv)
{
Fury fury_in_global_namespace;
Green::Fury fury_in_Green_namespace;
return 0;
}
 
P

Peter van Merkerk

Agent Mulder said:
I have problems moving my project
into a seperate namespace.
Namespace introduces its own level
of bugs.

I'm afraid you are the one introducing the bugs, don't blame your own
shortcommings on the language.
#include <iostream.h>

#include said:
class Fury{};//comment this out and it compiles
namespace Green
{
class Fury{};//or comment this out and it compiles
};
using namespace Green;//or comment this out and it compiles
int main(int argc,char**argv)
{
Fury fury;//or comment this out and it compiles
return 0;
}

Let me guess, you get an ambiguity error when you compile this? That seems
to be the only logical response one can expect from a compiler with this
code. You define one class in the global namespace, and another class with
the same name in the Green namespace. Then you say with "using namespace
Green;", don't just lookup identifiers in the global namespace, but also in
the Green namespace. So when the compiler has to instantiate a "Fury" class
in the main() fucntion, it has two possible candiates: ::Fury and
Green::Fury. Do you expect the compiler to just pick one at random? Be glad
that the compiler refuses to compile this code and forces you to explicitly
specify what you want.

Your example does demonstrate the problem with too liberate use of "using
namespace ..." statements. It is best to limit its use to the smallest
possible scope. If a namespace is only used within one function put the
"using namespace ..." statement inside that function, so that other function
are not affected. Also avoid putting "using namespace ..." statements into
header files.
 
R

Rolf Magnus

Agent said:
I have problems moving my project into a seperate namespace.
Namespace introduces its own level of bugs.

#include <iostream.h>

This is a non-standard header (which your example doesn't use btw.)
class Fury{};//comment this out and it compiles
namespace Green
{
class Fury{};//or comment this out and it compiles
};
using namespace Green;//or comment this out and it compiles

"using namespace" is a lot less useful that many people seem to think.
It's the main source of the problem you experience here.
int main(int argc,char**argv)
{
Fury fury;//or comment this out and it compiles
return 0;
}

What exactly did you expect instead?
 
A

Agent Mulder

JS> is there suppose to be a semi-colon after the closing of namespace
JS> declaration?

No, it's a typo. Thank you. I also meant
Furry instead of Fury.

-X

class Furry{};//comment this out and it compiles
namespace Green
{
class Furry{};//or comment this out and it compiles
}
using namespace Green;//or comment this out and it compiles
int main(int argc,char**argv)
{
Furry furry;//or comment this out and it compiles
return 0;
}

-X
 

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

Forum statistics

Threads
474,138
Messages
2,570,801
Members
47,347
Latest member
RebekahStu

Latest Threads

Top