473,395 Members | 1,815 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,395 software developers and data experts.

Does "using namespace std" cost ?

If I use just a STL container such as std::vector in my program.
Is "using std::vector;" better than "using namespace std" ?
Does "using namespace std" cost ?

Jul 23 '05 #1
4 2678

"Teddy" <du********@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
If I use just a STL container such as std::vector in my program.
Is "using std::vector;" better than "using namespace std" ?
Does "using namespace std" cost ?


"using namespace std" pulls everything from namespace std to the current
scope. If the current scope if small and managable enough (e.g. within a
small function) and you know you are not having name clashes, go for it. For
larger scope where more than one library is employed, using declarative is
preferred.
Jul 23 '05 #2
Teddy wrote:
If I use just a STL container such as std::vector in my program.
Is "using std::vector;" better than "using namespace std" ?
That's a matter of opinion and it depends on the circumstances. There
appears to be many people who avoid 'using namespace std;', and recommend to
others to avoid it, because it unnecessarily makes all names in the std
namespace visible at global scope, which can lead to inadvertent name
clashes. In practice, I suspect that this hardly ever happens, and if it
does you will almost certainly get a compiler error and easily fix the
problem. I've used it frequently for years and I've never had a problem,
even in large, complex files. I just don't see the point of creating a long
list of specific 'using's that you have to keep adding to every time you use
a new name when a single line takes care of it. The only thing I would
suggest is avoiding putting any 'using' at global scope in a header file,
because then individual source files that include the header lose control of
them.
Does "using namespace std" cost ?


In what sense 'cost'? The difference between them is the name lookup during
compilation.

DW
Jul 23 '05 #3
"Teddy" <du********@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
If I use just a STL container such as std::vector in my program.
Is "using std::vector;" better than "using namespace std" ?
Does "using namespace std" cost ?


Some, including me, would say that using std::vector (or is that using
namespace std:vector?) is better than using namespace std. Personally, I
don't use the using clause at all and just put std:: in front of everything
from the std namespace.

As for the "cost", the cost only comes at compile time, when you compile it.
It doesnt' effect run time whatsoever. It's just for name resolution during
compile.
Jul 23 '05 #4
Teddy wrote:
If I use just a STL container such as std::vector in my program.
Is "using std::vector;" better than "using namespace std" ?
Does "using namespace std" cost ?


It could cause name conflicts if you are using a same variable name in
your program that is available in the std namespace.

This should answer you:
http://www.parashift.com/c++-faq-lit....html#faq-27.5

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Justin | last post by:
Hi. I decided to tidy up some of my queries, but I came across something that stumpt me: mysql> SELECT -> jobs.jobId, -> jobs.active, -> jobs.title, -> jobs.listed, -> ...
5
by: cppaddict | last post by:
It is typical to put the line: using namespace std; at the top of a file which makes use of std library objects. To take a simple example: #include <iostream> using namespace std;
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
13
by: Squid Seven | last post by:
This is just bizarre. for the following snippet of code: #include <string> using std::string; I get the error message:
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
12
by: Steve Pope | last post by:
Compiling the following works on my system: file main.cpp: #include <iostream> namespace space { int foo; }
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.