Namespaces

  • Thread starter Marcin Vorbrodt
  • Start date
M

Marcin Vorbrodt

Here is an example of my code:

//Header file
#include <vector>
using std::vector;

namespace Revelation {
// class definitions, etc...
// class members are of type std::vector
}


//Source File
#include <header_file>
namespace Revelation {
// stuff that used std::vector here;
}

Now my question is: where should I put the #include<vector>, and where
should I put using std::vector ??? Both outside of my namespace? Include
outside and "using" inside? Both inside?

Thanks,
Martin
 
A

Alf P. Steinbach

Here is an example of my code:

//Header file
#include <vector>
OK.


using std::vector;

Don't do that in a header file.


namespace Revelation {
// class definitions, etc...
// class members are of type std::vector
}


//Source File
#include <header_file>
namespace Revelation {
// stuff that used std::vector here;
}

Now my question is: where should I put the #include<vector>,

OK as is.

and where should I put using std::vector ???

Not in the header file.

Before the code that uses 'std::vector' unqualified.
 
M

Marcin Vorbrodt

Let me be more specific...
Should the #unclude and using be inside my namespace or before it?

Martin

 
A

Alf P. Steinbach

Answer: Because it messes up the order in which people normally read text.

Question: Why is top-posting such a bad thing?

Answer: Top-posting.

Question: What is the most annoying thing on Usenet?



Let me be more specific...
Should the #unclude and using be inside my namespace or before it?

Martin
 
M

Marcin Vorbrodt

OK...

Alf P. Steinbach said:
Answer: Because it messes up the order in which people normally read text.

Question: Why is top-posting such a bad thing?

Answer: Top-posting.

Question: What is the most annoying thing on Usenet?
 
P

Peter van Merkerk

</Marcin Vorbrodt>

Namespace is a fairly useless feature since you drag
all names back into the global namespace before you
can use them. With some extra typing and an extra
level of bugs.

The fact that you don't understand namespaces doesn't make them useless.
The only good thing about namespace is the global
namespace :: It makes you feel like you are 'inside
an object' when programming.

If you sincerely believe that is a good advice, you better stay far away
from large scale C++ projects and stick with "hello world" type of
applications. Considering that many programming languages offer a feature
similar to C++ namespaces should ring a bell though.
 
A

Agent Mulder

The one good thing about namespace is the global
namespace :: It makes you feel like you are 'inside
an object' when programming.
</Agent Mulder>

If you sincerely believe that is a good advice, you better stay far away
from large scale C++ projects and stick with "hello world" type of
applications. Considering that many programming languages offer a feature
similar to C++ namespaces should ring a bell though.
</Peter van Merkerk>

Why not declare a class LargeScaleC++Project with pointers
to (named) MiddleScaleC++Project objects? With proper
design there is no need for namespace. If such thing were
introduced at all, it had been better to add file level support.

-X
 
M

Mike Wahler

Agent Mulder said:
</Marcin Vorbrodt>

Namespace is a fairly useless feature
Nonsense.

since you drag
all names back into the global namespace before you
can use them.


// the following uses a single name from namesspace
// 'std'. It puts no names at all in the 'global
// namespace' except the function name 'foo()'

#include <iostream>

void foo()
{
using std::cout;
cout << "you don't understand namespaces\n";
}


The following puts all names from namespace 'std'
in the scope of function 'foo()', and puts no names
in 'global scope' except the function name 'foo()'

#include <iostream>

void foo()
{
using namespace std;
cout << "you need to read about namespaces in a C++ book\n";
}

With some extra typing

One cannot use a language feature without expressing it.
This is done by typing. How else?
and an extra
level of bugs.

What level of bugs? You mean those that are introduced
by folks who don't understand namespaces?
The only good thing about namespace is the global
namespace ::

What's good about it?
It makes you feel like you are 'inside
an object' when programming.

And that's good because? What does it feel like to
be 'inside an object?'

-Mike
 
P

Peter van Merkerk

</Agent Mulder>


</Peter van Merkerk>

Why not declare a class LargeScaleC++Project with pointers
to (named) MiddleScaleC++Project objects?

If you have to ask that you are really clueless about namespaces and even
more so about large scale projects. Classes and namespaces address two very
different issues. A class LargeScaleC++Project with pointers to
MiddleScaleC++Project objects (to which instances are they pointing too???)
does something totally different than namespaces. It seems that you don't
know the difference between declaration and instantiation. In the days
namespaces were not commonly supported by C++ compilers sometimes a struct
was used to emulate namespaces (but not in the way you describe). However
that struct hack was a long trek from the real thing.
With proper
design there is no need for namespace.

This has little to do with design. But if you never go beyond "hello world"
type applications namespaces are indeed a useless feature.
If such thing were
introduced at all, it had been better to add file level support.

I don't know what you mean by that (it could mean just about anything), but
I guess the standardization commitee should invite you to their next
meeting.
 
A

Agent Mulder

Mike Wahler said:
// the following uses a single name from namesspace
// 'std'. It puts no names at all in the 'global
// namespace' except the function name 'foo()'

#include <iostream>

void foo()
{
using std::cout;
cout << "you don't understand namespaces\n";
}


The following puts all names from namespace 'std'
in the scope of function 'foo()', and puts no names
in 'global scope' except the function name 'foo()'

#include <iostream>

void foo()
{
using namespace std;
cout << "you need to read about namespaces in a C++ book\n";
}



One cannot use a language feature without expressing it.
This is done by typing. How else?


What level of bugs? You mean those that are introduced
by folks who don't understand namespaces?


What's good about it?


And that's good because? What does it feel like to
be 'inside an object?'

-Mike

Few can claim to understand namespace. And once I understand
it, does it mean that I will like it?

-X
 
B

Bob Jacobs

Agent Mulder said:
Few can claim to understand namespace.

I can't imagine why you think that's true. Namespaces are a simple concept.
I'm sure many people understand them.
 
M

Mike Wahler

Agent Mulder said:
Few can claim to understand namespace.

You need to get out more. :)

I understand namespaces. Many, many others do as well.
I supect that among those who try, the majority do understand
them. It's not really a very complex issue.
And once I understand
it, does it mean that I will like it?

Perhaps, perhaps not. It really depends upon how you'll
be using C++. If you work on large projects, and/or use
one or more third-party libraries, namespaces make life
*much* easier. I remember working on C projects using
multiple libraries, and had to waste time hacking the header
files of one or more of the libraries in order to resolve
name clashes. This also makes the code base more 'fragile'.
C++ namespaces eliminate this problem.

-Mike
 
A

Agent Mulder

</Agent Mulder>

I understand namespaces. Many, many others do as well.
I supect that among those who try, the majority do understand
them. It's not really a very complex issue.
</Mike Wahler>

To deepen your understanding of namespace I copied from a
posting from Scott Meyers at

http://www.google.de/[email protected]
m

<Scott Meyers>
Those of you who are familiar with
the history of namespaces will recall that Koenig lookup was added when it
became clear that the name lookup rules introduced with the first draft
semantics of namespaces led to programs failing (due to name lookup
failures) that everybody wanted to succeed. Like magic, Koenig lookup
makes them succeed. But it's a double-edged sword. Koenig lookup also
makes some calls fail that we would like to succeed -- calls that would
succeed if namespaces didn't exist.

Here's my summary of the history of namespaces:
Namespaces Version 0 (no Koenig lookup): Oops, some things fail that
shouldn't...
Namespaces Version 1 (Koenig lookup): No problem, we can hack it
to make it work.
Namespaces Version 2 (whatever Hmmm, that hack didn't work
comes next): as we thought it would.
Maybe this will fix it...

Don't get me wrong, I have tremendous respect for the people who have been
working on namespaces, only one of whom is named Koenig. I just think that
namespaces are an experiment that failed. In Pete's terms, I don't think
the benefits of the practice justify the costs. Others almost certainly
disagree.
</Scott Meyers>
 
M

Mike Wahler

Agent Mulder said:
</Agent Mulder>


</Mike Wahler>

To deepen your understanding of namespace I copied from a
posting from Scott Meyers at

Why do you feel my understanding of namespaces is not 'deep' enough?
Also, the text you quote below is not an explanation of namespaces,
but only Scott's opinion about their success/failure.

What specific problems do namespaces cause for you?
http://www.google.de/[email protected]

I see nothing at that link but what appears to be a very
long email address.

From the text you quoted:

"In Pete's terms, I don't think the benefits of the practice
justify the costs. Others almost certainly disagree."

Who is Pete, and what are his 'terms'? What 'practice' is
Scott talking about? What 'costs' is he talking about?

Also note that Scott acknowledges the fact that he's only
stating his opinion, and that he feels that many if not
most don't share his opinion ("Others almost certainly disagree.")

-Mike
 
A

Agent Mulder

<Mike Wahler>
Who is Pete, and what are his 'terms'? What 'practice' is
Scott talking about? What 'costs' is he talking about?
</>

A lot of questions. I never claimed to understand namespace.

-X
 
D

dwrayment

I dont normally get invovled in these childish arguments, but in this case i
feel the need.

To me the ablity to use my own namespace is on the the most important
features of C++, and to anyone who wants to argue try designing you own
classes for strings or any generic class. Now you can try to come up with
some stupid code like

class mystring;

or you can use a namespace like

namespace MyString {

class string;

}

Now suppose you import someone elses library and that lib has its own string
class. You going to get to many cant choose which string compile errors.
Without namespaces its compile hell.
With namespaces wherever the two classes clash you can use
MyString::string.

I have my own dll with several generic classes and its good to know that i
can import it to any project and not get any compile errors since its
encapsalated into its own namespace.

To those who argue namespaces are useless, I say wait till your
first project where you have to rename an entire library because the
function names are defined elsewhere, Ive done that ages ago before C++ and
it aint fun.
 
M

Mike Wahler

Agent Mulder said:
<Mike Wahler>
Who is Pete, and what are his 'terms'? What 'practice' is
Scott talking about? What 'costs' is he talking about?
</>

A lot of questions.

That's what happens when you quote someone without
full context. :)
I never claimed to understand namespace.

Well, if you want to, do some reading. If you don't
understand what you read, ask about it here.

There may indeed have been some 'glitches' when the
ISO committee first tried to implement namespaces,
but the concept is imo not very complex.

-Mike
 
D

dwrayment

please dont bore me with this shit. i wil say whatever i like, if you dont
like it you dont have to read it.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top