By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Namespaces

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
24 Replies


P: n/a
On Sat, 6 Sep 2003 01:20:47 -0400, "Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote:
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.

Jul 19 '05 #2

P: n/a
Let me be more specific...
Should the #unclude and using be inside my namespace or before it?

Martin

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Sat, 6 Sep 2003 01:20:47 -0400, "Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote:
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.

Jul 19 '05 #3

P: n/a
On Sat, 6 Sep 2003 02:00:04 -0400, "Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote:

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

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Sat, 6 Sep 2003 01:20:47 -0400, "Marcin Vorbrodt"

<mv*****@eos.ncsu.edu> wrote:
>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.



Jul 19 '05 #4

P: n/a
OK...

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Sat, 6 Sep 2003 02:00:04 -0400, "Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote:
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

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Sat, 6 Sep 2003 01:20:47 -0400, "Marcin Vorbrodt"

<mv*****@eos.ncsu.edu> wrote:

>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.


Jul 19 '05 #5

P: n/a
Marcin Vorbrodt wrote:
OK...


Please don't top-post. It is bad Usenet etiquette. Re-read section 5 of
the FAQ for posting guidelines. You seem to have missed some points the
first time through.

http://www.parashift.com/c++-faq-lite/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6

P: n/a
> <Marcin Vorbrodt>
" <mv*****@eos.ncsu.edu> wrote in message
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? </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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #7

P: n/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>

<Peter van Merkerk> 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
Jul 19 '05 #8

P: n/a

Agent Mulder <mb*******************@home.nl> wrote in message
news:bj**********@news2.tilbu1.nb.home.nl...

<Marcin Vorbrodt>
" <mv*****@eos.ncsu.edu> wrote in message
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? </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

Jul 19 '05 #9

P: n/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>

<Peter van Merkerk>
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?


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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Jul 19 '05 #10

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:aw***************@newsread4.news.pas.earthlin k.net...

Agent Mulder <mb*******************@home.nl> wrote in message
news:bj**********@news2.tilbu1.nb.home.nl...

<Marcin Vorbrodt>
" <mv*****@eos.ncsu.edu> wrote in message
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?

</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


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

-X
Jul 19 '05 #11

P: n/a

"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bj**********@news2.tilbu1.nb.home.nl...

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.
Jul 19 '05 #12

P: n/a
Agent Mulder <mb*******************@home.nl> wrote in message
news:bj**********@news2.tilbu1.nb.home.nl...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:aw***************@newsread4.news.pas.earthlin k.net...

Agent Mulder <mb*******************@home.nl> wrote in message
news:bj**********@news2.tilbu1.nb.home.nl...

<Marcin Vorbrodt>
" <mv*****@eos.ncsu.edu> wrote in message
> 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?
</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


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

Jul 19 '05 #13

P: n/a
<Agent Mulder>
Few can claim to understand namespace.
</Agent Mulder>

<Mike Wahler> 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/groups?selm=MPG...ews.hevanet.co
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>

Jul 19 '05 #14

P: n/a

Agent Mulder <mb*******************@home.nl> wrote in message
news:bj**********@news4.tilbu1.nb.home.nl...
<Agent Mulder>
Few can claim to understand namespace.
</Agent Mulder>

<Mike Wahler>
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


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/groups?selm=MPG...ews.hevanet.co

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

Jul 19 '05 #15

P: n/a
<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


Jul 19 '05 #16

P: n/a
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.


"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bj**********@news2.tilbu1.nb.home.nl...

<Marcin Vorbrodt>
" <mv*****@eos.ncsu.edu> wrote in message
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?

</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 only good thing about namespace is the global
namespace :: It makes you feel like you are 'inside
an object' when programming.

-X

Jul 19 '05 #17

P: n/a

Agent Mulder <mb*******************@home.nl> wrote in message
news:bj**********@news1.tilbu1.nb.home.nl...
<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

Jul 19 '05 #18

P: n/a
dwrayment wrote:
I dont normally get invovled in these childish arguments,


Well, when you do, please don't top-post your response. Re-read section
5 of the FAQ for posting guidelines. You might also be interested in RFC
1855 (Netiquette guidelines) section 3.

http://www.parashift.com/c++-faq-lite/
http://www.dtcc.edu/cs/rfc1855.html

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #19

P: n/a
please dont bore me with this shit. i wil say whatever i like, if you dont
like it you dont have to read it.
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:4P****************@newsread4.news.pas.earthli nk.net...
dwrayment wrote:
I dont normally get invovled in these childish arguments,


Well, when you do, please don't top-post your response. Re-read section
5 of the FAQ for posting guidelines. You might also be interested in RFC
1855 (Netiquette guidelines) section 3.

http://www.parashift.com/c++-faq-lite/
http://www.dtcc.edu/cs/rfc1855.html

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #20

P: n/a
dwrayment wrote:
please dont bore me with this shit. i wil say whatever i like, if you dont
like it you dont have to read it.


That's right, I don't.

*PLONK*

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #21

P: n/a

"Peter van Merkerk" <me*****@deadspam.com> wrote in message
news:bj************@ID-133164.news.uni-berlin.de...

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.


Well, usually. The use of static public members in a class amounts to a
namespace.
Jul 19 '05 #22

P: n/a

"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bj**********@news2.tilbu1.nb.home.nl...

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


It's generally poor form to argue that you don't like something until you
get it.
Jul 19 '05 #23

P: n/a
<Agent Mulder>
And once I understand namespace, does it mean that I will like it?
</>

<jeffc> It's generally poor form to argue that you don't like something until you
get it.

</>

A little rhetoric perhaps. But I have to resort
to rhetorics because I am afraid I'll never
understand namespace.

In VC++, for every unnamed namespace in
every compilation, a unique name is created
based on the day and the time of compilation,
up to the second. Isn't that horrible?

<Robert Schmidt>
Visual C++ synthesizes the hidden name from the source file's path and time/date of compilation. With such a naming algorithm, the
compiler minimizes the chance of two translation units using identical hidden names
</>

It is almost as bad as programmers comming up with their
home-grown #include guards :-)

Read more at

http://msdn.microsoft.com/library/de...ep08272002.asp
Jul 19 '05 #24

P: n/a

"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bj**********@news2.tilbu1.nb.home.nl...

In VC++, for every unnamed namespace in
every compilation, a unique name is created
based on the day and the time of compilation,
up to the second. Isn't that horrible?


The point that people are trying to make here is that namespaces solve some
common problems. The problem you mention I've never run into. I've run
into 3rd party name clashes many times. It's like you're being bitten by
mosquitoes in your American backyard, but you refuse to use bug spray
because you read somewhere that the bug spray doesn't work very well on some
particular strain of mosquito found in northern New Guinea. So what?
Listen, don't use it. Fine. One day if you start getting bit, then use it.
Jul 19 '05 #25

This discussion thread is closed

Replies have been disabled for this discussion.