364,036 Members | 5027 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

namespace convention confusion ?

vivekian
P: n/a
vivekian
Hi,

I am not sure if this is a relevant topic here, anyways,

When using the std namespace is it a good practice to ..

put this right at the beginning

using namespace std ;

or is prefixing std before each feature access e.g. std::string a
better thing to do ? The confusion because different books suggest
different things ..

thanks in advance,
vivekian

May 29 '06 #1
Share this Question
Share on Google+
6 Replies


Kai-Uwe Bux
P: n/a
Kai-Uwe Bux
vivekian wrote:
[color=blue]
> Hi,
>
> I am not sure if this is a relevant topic here, anyways,
>
> When using the std namespace is it a good practice to ..
>
> put this right at the beginning
>
> using namespace std ;
>
> or is prefixing std before each feature access e.g. std::string a
> better thing to do ? The confusion because different books suggest
> different things ..[/color]

The important consensus is to avoid "using namespace std;" in header files.


Best

Kai-Uwe Bux
May 29 '06 #2

Duane Hebert
P: n/a
Duane Hebert
> The important consensus is to avoid "using namespace std;" in header
files.

This would be true for any namespaces IMO.


May 29 '06 #3

Erik Wikström
P: n/a
Erik Wikström
On 2006-05-29 16:54, Kai-Uwe Bux wrote:[color=blue]
> vivekian wrote:
>[color=green]
>> Hi,
>>
>> I am not sure if this is a relevant topic here, anyways,
>>
>> When using the std namespace is it a good practice to ..
>>
>> put this right at the beginning
>>
>> using namespace std ;
>>
>> or is prefixing std before each feature access e.g. std::string a
>> better thing to do ? The confusion because different books suggest
>> different things ..[/color]
>
> The important consensus is to avoid "using namespace std;" in header files.[/color]

Something in between would be to use "using std::string;" which lets you
access string without having to prefix it with std:: without dragging
everything in std:: into your namespace.

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
May 29 '06 #4

osmium
P: n/a
osmium
"vivekian" writes:
[color=blue]
> When using the std namespace is it a good practice to ..
>
> put this right at the beginning
>
> using namespace std ;
>
> or is prefixing std before each feature access e.g. std::string a
> better thing to do ? The confusion because different books suggest
> different things ..[/color]

I keep namespaces out of header files and prefix each translation unit with
using namespace std;. This does have the hazard that the language gurus
have used some word I want to use for an identifier and took first cut at
it. I have resigned myself to unintelligible error messages so that can be
pretty darn annoying to find when it happens. As long as you keep in mid
that this might be happening, I think you are OK. I will concede there
might be giant programs with fifty or so programmers, where this might be a
bad choice.

I have been tempted to make a new "kind" of header file with the using
declarations I, personally, typically use and am aware of. It would,
necessarily, be the last header file to be included.





May 29 '06 #5

Cy Edmunds
P: n/a
Cy Edmunds

"vivekian" <viveklinux@gmail.com> wrote in message
news:1148913257.282697.113060@38g2000cwa.googlegro ups.com...[color=blue]
> Hi,
>
> I am not sure if this is a relevant topic here, anyways,
>
> When using the std namespace is it a good practice to ..
>
> put this right at the beginning
>
> using namespace std ;
>
> or is prefixing std before each feature access e.g. std::string a
> better thing to do ? The confusion because different books suggest
> different things ..
>
> thanks in advance,
> vivekian
>[/color]

I personally don't write "using namespace std;" in my code at all. I think
the five extra characters in each identifier are a small price to pay to
clarify where each symbol is coming from and to prevent name collisions.
Keep in mind std is a VERY big namespace and will grow with each major
update of the standard.


May 29 '06 #6

Jim Langston
P: n/a
Jim Langston
"vivekian" <viveklinux@gmail.com> wrote in message
news:1148913257.282697.113060@38g2000cwa.googlegro ups.com...[color=blue]
> Hi,
>
> I am not sure if this is a relevant topic here, anyways,
>
> When using the std namespace is it a good practice to ..
>
> put this right at the beginning
>
> using namespace std ;
>
> or is prefixing std before each feature access e.g. std::string a
> better thing to do ? The confusion because different books suggest
> different things ..[/color]

The FAQ answers this about using namespace std;
http://www.parashift.com/c++-faq-lit....html#faq-27.5



May 30 '06 #7

Post your reply

Help answer this question



Didn't find the answer to your C / C++ question?

You can also browse similar questions: C / C++