473,789 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2711

"Teddy" <du********@hot mail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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********@hot mail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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
1803
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, -> industries.industry, -> occupations.occupation,
5
1953
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
3518
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 using std::cin; using std::cout;
8
4377
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 somebody explain it to me? I am using MSVC 6.0. Thanks a lot,
13
141266
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
4123
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" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
12
2411
by: Steve Pope | last post by:
Compiling the following works on my system: file main.cpp: #include <iostream> namespace space { int foo; }
25
2593
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 example: with quitCommandButton .enabled = true .default = true end with
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9984
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.