473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Namespaces

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
24 3454
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
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
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
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
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
> <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
<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

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

"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

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

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

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

"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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: Mike Morse | last post by:
What see sample that show xs:element where the xs namespace = http://www.w3.org/2001/XMLSchema However, I see another example with xsi: where xsi = http://www.w3.org/2001/XMLSchema-instance ...
3
by: Jim Heavey | last post by:
Trying to get the hang of Namespaces. I have primarly developed in VB and am transitioning to C# and the .Net Environment. I have worked a bit with Java as well in school about a year or so ago....
17
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.