Quick Questions:
When and why should you use the īstdī namespace ?
What is the difference in writing std::cout and cout ?
Is the std namespace only used with the STL ?
Thanks,
bigSteve
Quick Questions:
When and why should you use the īstdī namespace ?
What is the difference in writing std::cout and cout ?
Is the std namespace only used with the STL ?
Thanks,
bigSteve
bigSteve
>When and why should you use the īstdī namespace ?
When: When you need to use a name in the standard library. Otherwise you would have trouble since they are all nested inside the std namespace.
Why: Because the C++ standard says so, and forces you to if you want to use the standard library.
>What is the difference in writing std::cout and cout ?
std::cout is guaranteed to work. cout may or may not work depending on whether or not there is a using directive or declaration visible. One of the two following:
>Is the std namespace only used with the STL ?Code:using namespace std; using std::cout;
No, the std namespace is used with the whole of the standard library.
My best code is written with the delete key.
Hmmm.....I thought the std namespace was already defined in the header file.
I've been wondering why they decided to go this route? Why make the standard library harder/confusing to use? Was it to alleviate problems if you wish to define your own components with the same name as a replacement?Originally posted by Prelude
Why: Because the C++ standard says so, and forces you to if you want to use the standard library.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
>I've been wondering why they decided to go this route?
The C++ standard library has a lot of names and most programmers aren't aware of them all. This creates a big risk for collisions. That is the primary reason why everything is in the std namespace. The committee debated whether or not to use one big namespace or several, but ended up deciding that splitting up the library into several namespaces wouldn't be an advantage over a single std namespace mainly because it would require the programmer to remember where everything came from, and it would be very chaotic. And how would you structure a namespace hierarchy without changing the structure of the library? The way I see it, there is no reasonable solution to that problem, so a single std namespace was used.
My best code is written with the delete key.
So as I surmised it's to allow "collision" names or to easily rewrite a module/method and not have to rename it.
Thanks for the explanation. Makes sense, at least to a degree.
One thing came to mind while reading your explanation:
Like stdio.h, stdlib.h, string.h, as nauseumsplitting up the library into several namespaces wouldn't be an advantage over a single std namespace mainly because it would require the programmer to remember where everything came from
Last edited by WaltP; 01-19-2004 at 09:50 PM.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
Or to put it another way, namespaces are just another way to define scope. It really is as simple as that.
As for my comment above, in the header file for iostream (the one provided with MS software) they place the std namespace into scope with the using namespace std; declaration. Thus you do not need to make this statement again in your code.
Sometimes compilers put that into the old headers (e.g. <iostream.h>) along with an include of the new header to make it appear as though they are backwards compatible with the non-standard libraries. However, it is probably a better idea to use the standard headers (e.g. <iostream>) and not count on the using declaration being there because a) it was bad form (or maybe non-standard) to put it into the new header if an implementation actually did that; and b) you can't count on that happening with any other library implementation.Originally posted by Traveller
As for my comment above, in the header file for iostream (the one provided with MS software) they place the std namespace into scope with the using namespace std; declaration. Thus you do not need to make this statement again in your code.
And even then, of course, they're not guaranteed to 'work' because of the possibility of name clashes. Only a namespace prefix gives you any kind of guarantee.Originally posted by Prelude
std::cout is guaranteed to work. cout may or may not work depending on whether or not there is a using directive or declaration visible. One of the two following:
Code:using namespace std; using std::cout;