namespace convention confusion ?

V

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
 
K

Kai-Uwe Bux

vivekian said:
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 ..

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


Best

Kai-Uwe Bux
 
D

Duane Hebert

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

This would be true for any namespaces IMO.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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

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
 
O

osmium

vivekian said:
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 ..

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.
 
C

Cy Edmunds

vivekian said:
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

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.
 
J

Jim Langston

vivekian said:
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 ..

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top