I attended a session in which it was taught that we should not use "using namespace std", instead do "std::cout" for using some call of the std namespace as this shall increase size of the binary
I tried verifying the same with the following experiment. The code & its output is as follows:-
[Fooo@EXP]$ cat namespacestd.cpp
#include<iostream>
#ifdef STD
using namespace std;
#endif
int main()
{
#ifndef STD
std::cout<<"\n ==> Workign \n";
#else
cout<<"\n ==> Workign \n";
#endif
return 0;
}
[Fooo@EXP]$ time g++ -c namespacestd.cpp -DSTD
real 0m0.246s
user 0m0.215s
sys 0m0.030s
[Fooo@EXP]$ size namespacestd.o
text data bss dec hex filename
310 8 1 319 13f namespacestd.o
[Fooo@EXP]$ time g++ -c namespacestd.cpp
real 0m0.258s
user 0m0.224s
sys 0m0.034s
[Fooo@EXP]$ size namespacestd.o
text data bss dec hex filename
310 8 1 319 13f namespacestd.o
[Fooo@EXP]$ time g++ -o namespacestd namespacestd.cpp -DSTD
real 0m0.293s
user 0m0.251s
sys 0m0.042s
[Fooo@EXP]$ size namespacestd
text data bss dec hex filename
1980 580 288 2848 b20 namespacestd
[Fooo@EXP]$ time g++ -o namespacestd namespacestd.cpp
real 0m0.274s
user 0m0.239s
sys 0m0.035s
[Fooo@EXP]$ size namespacestd
text data bss dec hex filename
1980 580 288 2848 b20 namespacestd
[Fooo@EXP]$
As I see from my experiment that
there is no effect on the size of binary
only
there is a difference in the compilation time.
Kindly correct me if my conclusions are flawed
Thanks
using namespace std shouldn’t affect binary size with most compilers. It should still be avoided for another reason:
The namespace std is really big. There are literally thousands of identifier in there which are all in the scope of your program. This increases the likeliness of collisions with your own identifiers or identifiers from other libraries which can cause some nasty surprises.
See also this related question: Why is "using namespace std" considered bad practice?