Hi all,
Since I am always confusing this, I want to know once and for all what
is the right way of doing this.
I've noticed that some programs use:
std::cout<< "yadayada"<<end l;
[which I'll call it alternative 1]
while some others use:
cout<< "yadayada"<<end l;
usually they have:
using namespace std;
somewhere.
[which I'll call it alternative 2]
now, I can understand that both has the same effect since the "using
namespace" is somehow equivelant to std:: before the cout.
I am more of an alternative 2 person but every now and then it does not
work untill I explicitly type the std:: before the statement.
what are the pros and cons of using each of the alternatives?
what should I use?
Thanks. 7 4545
On 2006-06-01 11:49, zahy[dot]bnaya[At]gmail[dot]com wrote: Hi all, Since I am always confusing this, I want to know once and for all what is the right way of doing this.
I've noticed that some programs use:
std::cout<< "yadayada"<<end l; [which I'll call it alternative 1]
Pros: You know what you get.
Cons: A bit more to type.
while some others use:
cout<< "yadayada"<<end l;
usually they have:
using namespace std; somewhere. [which I'll call it alternative 2]
Pros: Little less to type.
Cons: Might introduce weird problems. Not recommended.
The problem with "using namespace std;" is that you drag everything from
std into your namespace which might cause problems when there is a
function in std:: that has the same name as one of your functions. I
usually use the third method, "using std::cout;" which lets you use cout
without typing std:: in front of it every time and avoids the problems
with "using namespace std;".
For a more throughout explanation see the faq: http://www.parashift.com/c++-faq-lit....html#faq-27.5
Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
zahy[dot]bnaya[At]gmail[dot]com wrote: Hi all, Since I am always confusing this, I want to know once and for all what is the right way of doing this.
I've noticed that some programs use:
std::cout<< "yadayada"<<end l; [which I'll call it alternative 1]
while some others use:
cout<< "yadayada"<<end l;
usually they have:
using namespace std; somewhere. [which I'll call it alternative 2]
now, I can understand that both has the same effect since the "using namespace" is somehow equivelant to std:: before the cout.
I am more of an alternative 2 person but every now and then it does not work untill I explicitly type the std:: before the statement.
what are the pros and cons of using each of the alternatives? what should I use?
Thanks.
General rule: never put this statement in a header file;
Advice: use it where ever else you wish - your compiler will tell you
when you have namespace resolution conflicts. If you get weird
inexplicable errors, comment it out, put std:: in try again.
--
--dakka
Dykstra's Observation:
If debugging is the process of removing bugs, then programming must be
the process of putting them in.
Erik Wikström wrote: On 2006-06-01 11:49, zahy[dot]bnaya[At]gmail[dot]com wrote: Hi all, Since I am always confusing this, I want to know once and for all what is the right way of doing this.
I've noticed that some programs use:
std::cout<< "yadayada"<<end l; [which I'll call it alternative 1]
Pros: You know what you get. Cons: A bit more to type.
while some others use:
cout<< "yadayada"<<end l;
usually they have:
using namespace std; somewhere. [which I'll call it alternative 2]
Pros: Little less to type. Cons: Might introduce weird problems. Not recommended.
The problem with "using namespace std;" is that you drag everything from std into your namespace which might cause problems when there is a function in std:: that has the same name as one of your functions. I usually use the third method, "using std::cout;" which lets you use cout without typing std:: in front of it every time and avoids the problems with "using namespace std;".
I see people repeating the argument about dragging everything from
namespace std into the global namespace with "using namespace std". In
reality only what you actually include (directly or indirectly) from
the standard library becomes visible. This makes a significant
difference I would say even so the issue in general still remains.
Markus Schoder wrote: I see people repeating the argument about dragging everything from namespace std into the global namespace with "using namespace std". In reality only what you actually include (directly or indirectly) from the standard library becomes visible.
Yes. But since every standard header potentially includes every other
header,
this means you have to assume all names can be dragged in. This is a
difference
with C.
HTH,
Michiel Salters
"Dakka" <"news at electro dot mine dot nu"> wrote in message
news:44******** **************@ per-qv1-newsreader-01.iinet.net.au ... zahy[dot]bnaya[At]gmail[dot]com wrote: Hi all, Since I am always confusing this, I want to know once and for all what is the right way of doing this.
I've noticed that some programs use:
std::cout<< "yadayada"<<end l; [which I'll call it alternative 1]
while some others use:
cout<< "yadayada"<<end l;
usually they have:
using namespace std; somewhere. [which I'll call it alternative 2]
now, I can understand that both has the same effect since the "using namespace" is somehow equivelant to std:: before the cout.
I am more of an alternative 2 person but every now and then it does not work untill I explicitly type the std:: before the statement.
what are the pros and cons of using each of the alternatives? what should I use? Thanks.
General rule: never put this statement in a header file;
That I certainly agree with.
Advice: use it where ever else you wish - your compiler will tell you when you have namespace resolution conflicts. If you get weird inexplicable errors, comment it out, put std:: in try again.
-- --dakka
[snip]
I think your Advice is poor. Let's say you compile and test your code and
find no namespace conflicts. So far, so good. Now something happens:
1) the code is ported to a new platform
2) the code is recompiled with a different compiler (which may comprehend a
modified standard)
3) the code needs to be recompiled because of a change in a non-standard
header
4) the code needs to be recompiled because it has been changed
In any of these cases name conflicts might suddenly appear. And whoever gets
the error messages isn't going to thank you for trying to save yourself a
few keystrokes.
Maintainability is key in software development. Keystrokes are nothing.
Think of the last major project you worked on. What fraction of the time was
taken by the actual typing?
Cy
Cy Edmunds wrote: "Dakka" <"news at electro dot mine dot nu"> wrote in message news:44******** **************@ per-qv1-newsreader-01.iinet.net.au ... zahy[dot]bnaya[At]gmail[dot]com wrote: Hi all, Since I am always confusing this, I want to know once and for all what is the right way of doing this.
I've noticed that some programs use:
std::cout<< "yadayada"<<end l; [which I'll call it alternative 1]
while some others use:
cout<< "yadayada"<<end l;
usually they have:
using namespace std; somewhere. [which I'll call it alternative 2]
now, I can understand that both has the same effect since the "using namespace" is somehow equivelant to std:: before the cout.
I am more of an alternative 2 person but every now and then it does not work untill I explicitly type the std:: before the statement.
what are the pros and cons of using each of the alternatives? what should I use? Thanks. General rule: never put this statement in a header file;
That I certainly agree with.
Advice: use it where ever else you wish - your compiler will tell you when you have namespace resolution conflicts. If you get weird inexplicable errors, comment it out, put std:: in try again.
-- --dakka [snip]
I think your Advice is poor. Let's say you compile and test your code and find no namespace conflicts. So far, so good. Now something happens:
1) the code is ported to a new platform 2) the code is recompiled with a different compiler (which may comprehend a modified standard) 3) the code needs to be recompiled because of a change in a non-standard header 4) the code needs to be recompiled because it has been changed
In any of these cases name conflicts might suddenly appear. And whoever gets the error messages isn't going to thank you for trying to save yourself a few keystrokes.
Maintainability is key in software development. Keystrokes are nothing. Think of the last major project you worked on. What fraction of the time was taken by the actual typing?
Cy
Doubtful. Remember that std namespace uses well known members and
identifiers that for the most part seamlessly port. As I said, when it
doesn't compilers let you know. If the using statement resides in a
compilation unit it is easy to remove it as I said. Your comments are
more relevant with custom namespaces.
--
--dakka
Dykstra's Observation:
If debugging is the process of removing bugs, then programming must be
the process of putting them in.
"Dakka" <"news at electro dot mine dot nu"> wrote in message
news:44******** *************** @per-qv1-newsreader-01.iinet.net.au ... Cy Edmunds wrote: "Dakka" <"news at electro dot mine dot nu"> wrote in message news:44******** **************@ per-qv1-newsreader-01.iinet.net.au ... zahy[dot]bnaya[At]gmail[dot]com wrote: Hi all, Since I am always confusing this, I want to know once and for all what is the right way of doing this.
I've noticed that some programs use:
std::cout<< "yadayada"<<end l; [which I'll call it alternative 1]
while some others use:
cout<< "yadayada"<<end l;
usually they have:
using namespace std; somewhere. [which I'll call it alternative 2]
now, I can understand that both has the same effect since the "using namespace" is somehow equivelant to std:: before the cout.
I am more of an alternative 2 person but every now and then it does not work untill I explicitly type the std:: before the statement.
what are the pros and cons of using each of the alternatives? what should I use? Thanks.
General rule: never put this statement in a header file;
That I certainly agree with.
Advice: use it where ever else you wish - your compiler will tell you when you have namespace resolution conflicts. If you get weird inexplicable errors, comment it out, put std:: in try again.
-- --dakka [snip]
I think your Advice is poor. Let's say you compile and test your code and find no namespace conflicts. So far, so good. Now something happens:
1) the code is ported to a new platform 2) the code is recompiled with a different compiler (which may comprehend a modified standard) 3) the code needs to be recompiled because of a change in a non-standard header 4) the code needs to be recompiled because it has been changed
In any of these cases name conflicts might suddenly appear. And whoever gets the error messages isn't going to thank you for trying to save yourself a few keystrokes.
Maintainability is key in software development. Keystrokes are nothing. Think of the last major project you worked on. What fraction of the time was taken by the actual typing?
Cy
Doubtful. Remember that std namespace uses well known members and identifiers that for the most part seamlessly port. As I said, when it doesn't compilers let you know. If the using statement resides in a compilation unit it is easy to remove it as I said. Your comments are more relevant with custom namespaces.
I remember a while back someone posted in this newsgroup a problem he had
compiling a rather simple program. It turns out he was using namespace std;
and the class he was trying to create had the same name of something in the
std he was including. Compilers aren't usually very good at telling you
exactly what's wrong, only what they think is wrong. Sometimes they compile
anyway using hiding and you get weird results.
Also, "std namespace uses well known members" in a perfect world. This is
not a perfect world, especially with Windows compilers. Windows headers are
well known for adding stuff to the stl in std that doesn't belong.
This is one reason I'm one of those that never use use namespace std;, I'll
always just use the std:: everywhere, it's not that big of a deal. And yes,
this is in big projects. I've been programming many years and I'm one of
those that am very glad that C++ has namespaces, as I've run into name
collision quite a bit over the years and it can be frustrating. Seeing as
now we have it, I'm not going to short circuit it by bringing everything
into the unnamed namespace anyway. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
by: Randy Yates |
last post by:
I've seen this creep into c++ and have never heard of it. It apparently
has something to do with the standard C++ library header files (e.g., <string>,
<fstream>, etc.). Any illunination?
--
% Randy Yates % "My Shangri-la has gone away, fading like
%% Fuquay-Varina, NC % the Beatles on 'Hey Jude'"
%%%...
|
by: trying_to_learn |
last post by:
im in the primary stages of learning C++. The book im learning from says
Dont use using namespace.. directive in header file
However im trying to make the following header file.I need to include
<string> header file and also will have to say using namespace std to
make the string class visible.
qn)how do i resolve the requirement of *not*...
|
by: Petter Reinholdtsen |
last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to
compile a program using 'using namespace std;' at the top. The reason
seem to be that the compiler refuses to accept 'using namespace std;'
unless the std namespace was declared first.
This triggered my curiosity, and I tried to find out what the ANSI C++
standard had...
|
by: Enos Meroka |
last post by:
Hallo,
I am a student doing my project in the university.. I have been trying
to compile the program using HP -UX aCC compiler, however I keep on
getting the following errors.
=================================================================
Error 19: "CORBAManagerMessages.h", line 4 # Unexpected 'std'.
using std::string;
^^^
| |
by: Christian Christmann |
last post by:
Hi,
are there any drawbacks using
"using namespace std"
instead of defining the required namespaces like
using std::cout
|
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...
|
by: Animatorboy |
last post by:
Ok I am making a very simple program (I am new to this so I dont know
much, its kinda a trial and error thing)
This is what I am trying to do:
-Get a name from the user (designated as variable 'name')
-Take that name and say this to them
"Well, Helllo 'name' welcome.
Then from there I dont know what I wanna do, but this is what I have
|
by: Halcyon |
last post by:
so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.
I don't quite get what the potential...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |