Is this true

closed account (iAk3T05o)
Is using using namespace std; not good.
I just read an article in the C++ faq and it said that std:: (cout, string etc.) should be used rather than using namespace std; because it causes some kind of conflicts.
I think this link here explains why it is bad.

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

If I understood that correctly, the general reason people don't like using namespace std is if you upgrade a namespace at a later date, sometimes you may inadvertently duplicate functions in another namespace leading to errors and a headache when debugging.
Last edited on
It may cause comflict when you use it with some other nonstandard library.

Eg. Say a library haa a function cout. Using this function without specifying the namespace may cause conflict,

Aceix.
Is using using namespace std; not good.

Yes, it is bad style because it causes namespace pollution.
It must be avoided especially in headers.

example_header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef EXAMPLE_HEADER_H
#define EXAMPLE_HEADER_H

#include <string>

// ...

using namespace std;
// bad, because now all files including this header will have a
// using namespace std;
// line in them

// ...

#endif 


Writing std:: has the potential benefit of making your code easier to understand
This is because other programmers can recognize immediately what parts of the code are your own, and which are from the C++ library.

1
2
3
4
5
6
7
8
9
// ...

func(a, b); // provided by us
std::swap(a, b); // provided by C++ standard library

MyArray<int> ma(30); // provided by us
std::vector<int> vi(30); // provided by C++ standard library

// ... 
closed account (iAk3T05o)
Hmm. That's probably why it's Code::Blocks not CodeBlocks.
It's gonna be hard to learn c++ thinking about std::blah because i'm using the pdf of this site and there's no std::, so i won't know where it goes. Will it be std::int, std::while etc. Hmm
No. There are some basic C++ keywords such as while, return, int and many others that do not require the namespace qualification.

Anyway you can experiment.

Aceix.
Will it be std::int, std::while etc. Hmm

Those are part of the C++ language, not the C++ standard library.

The C++ standard library is composed of the things you use by including header files. You can find a list of header files here:
http://www.cplusplus.com/reference/
http://en.cppreference.com/w/cpp/header

The contents of the headers (listed in the above sites) are put in the std namespace.

You may want to read about namespaces:
http://www.cplusplus.com/doc/tutorial/namespaces/#namespace
closed account (iAk3T05o)
Out of all those header files, i only know <iostream>, <string> and <sstream>. So the rest is gibberish for now.
Is there any software that uses all/almost all those header files (i'm thinking a game engine and OS)
Probably not.
Most of the useful programs out there just use other libraries to get some help with portability tho.
But there's no standard C/C++ way to create a window.
There are options like GTK, QuickTime...
Also Networking's not in the C/C++ standard (It was proposed).
C++ standard doesn't even assume a console has a X/Y size.

EDIT: After reading Cubbi under me, I found out I re-interpreted your question as "Is there any software that uses only those header files".
Pointing that out, too lazy to rewrite the reply.
Last edited on
Is there any software that uses all/almost all those header files

Yes, many sufficiently complex applications use them all. Except maybe the C compatibility headers (ciso646, cstdbool, ctgmath, cstdalign, and ccomplex).. For example, in the application I'm working on, #include <ciso646> appears 25 times, but I am willing to bet nothing would break if I were to remove it - all our compilers are decent enough to not need it.

i'm thinking a game engine and OS

I'm not familiar with game engines, but most existing OSes are written in C, so they don't really use the C++ library.
Topic archived. No new replies allowed.