using namespace std; after #includes (was: C++ Homework. Please help :-))

using namespace std; after #includes (was: C++ Homework. Please help :-))

Post by blargg.ei » Sat, 28 Feb 2009 03:09:11



[...]

These are the two styles being compared:

#include <iostream>
using namespace std;

int main()
{
cout << "Hello!\n";
/* ... more uses of names in std ... */
}

and

#include <iostream>

int main()
{
std::cout << "Hello!\n";
/* ... more uses of names in std ... */
}

That is, a "using namespace std;" AFTER all #includes, and never in a
header file. What are the downsides of the second approach? The upside
is the lack of the repetitive std:: before every use of a standard name,
which can improve readability. There's no name clash cost imposed on
other clients because this is done in a source file.

One minor downside I can think of is that moving functions to the header
file for inlining will require addition of std:: prefixes. Also, not
being in the habit of regularly using std:: before all standard names
might cause one to accidentally use a member function of the same name
which is hiding the standard name at global scope.

--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

1. Is 'using namespace std;' valid without 'namespace std {};' first?

2. #include <iostream> & "using namespace std;"

Hi all,

I just started learning this spreme language of VC++ using
version 7.0.9037 on Visual Studio .NET (a bit buggy since
this is Beta 1). From my previous post I learnt that I
should use <iostream> instead of <iostream.h>.

Here is my VERY SIMPLE code;
+++
#include <iostream>

using namespace std;

int main (void)
{
cout << "Hello World!";
return 0;
}
+++

1.
The above-mentioned code works without a hitch, but if I
take out "#include <iostream>" directive or "using
namespace std;", the error message says, "error
C2065: 'cout' : undeclared identifier". Why do I need
both? I thought "std" embodies standard input and output
libraries and therefore I don't need #include directive...

2.
Why can I NOT find "std" header file under \Microsoft
Visual Studio.NET\Vc7\include\? All the other header files
are in this "include" folder. Is this "std" something very
special??? Where can I find it?

3.
What is your opinion in using "using namespace std;"
extravagantly? I read some articles saying that we can use
it for a small project, but not for a big one since it
might mask global variables scope unnecessarily (or
unexpectedly), in other words, they suggest
use "std::cout". What do you think?

Thanks for your time,
---
Tetsuya Oguma, Sydney, Australia

3. which headers to #include - std::ostream std::iostream std::iosfwd

4. **HELP**' C++ Runtime Library Error' (Please Help I am really STUC

5. Using C++ namespaces in C# namespace style

6. std::map<std::string, std::string> produces complie errors Managed C++ Class Library

7. Is the class iterator part of namespace std now in c++?

8. Please help: Query uses _old_ value, but I am using _AfterUpdate_

9. How to set the include directory for a project in VS.NET ( C++), please help

10. Why there is just one namespace 'std' in the std library?

11. BCB2006: memset, std namespace, std header idempotence, bug?

12. Convert std::string to std::wstring using std::ctype widen()

13. Q: Convert std::string to std::wstring using std::ctype widen()

14. how to include a c struct in C++ namespace