473,320 Members | 2,088 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,320 software developers and data experts.

Morality of std:: prefix

Hi,

I'm curious about the pros and cons of using the std:: prefix versus
the "using namespace std;" directive.

Obviously the directive saves you some typing, but I seem to remember
reading somewhere that it is not good form. Maybe because it is not
immediatly apparent which objects are part of the standard libraries
and which are your own.

Does anyone have additional thoughts on the matter or know what the
expert consensus is, assuming there is one?

Thanks,
cpp
Jul 22 '05 #1
7 2458
"cppaddict" <he***@hello.com> wrote...
I'm curious about the pros and cons of using the std:: prefix versus
the "using namespace std;" directive.
[..]
Does anyone have additional thoughts on the matter or know what the
expert consensus is, assuming there is one?


The consensus can be found (with some effort) on Gougle Groups. If
you really can't find it, perhaps reading some modern books on C++
written by experts might help. Even the FAQ has a couple of words
on coding standards (although none on 'using' versus 'std::') with
links to more online resources.

V
Jul 22 '05 #2
"cppaddict" <he***@hello.com> wrote...
I'm curious about the pros and cons of using the std:: prefix versus
the "using namespace std;" directive.
[..]
Does anyone have additional thoughts on the matter or know what the
expert consensus is, assuming there is one?


The consensus can be found (with some effort) on Gougle Groups. If
you really can't find it, perhaps reading some modern books on C++
written by experts might help. Even the FAQ has a couple of words
on coding standards (although none on 'using' versus 'std::') with
links to more online resources.

V
Jul 22 '05 #3
cppaddict wrote:
I'm curious about the pros and cons of using the std:: prefix versus
the "using namespace std;" directive.
Pro.
Obviously the directive saves you some typing, but I seem to remember
reading somewhere that it is not good form. Maybe because it is not
immediatly apparent which objects are part of the standard libraries
and which are your own.


Why should the language have a namespace feature if you are just going to
throw it away?

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #4
> Why should the language have a namespace feature if you are just going to
throw it away?


Why should my car have a ciggarette lighter "feature" if I'm not gonna use
it ;)

that's just me playing the devil's advocate by the way. I think "using"
using is probably not the way to go, although laziness more often than not
wins me over.

Jul 22 '05 #5

"Dan Moos" <da******@verizon.net> wrote in message news:ZJ******************@nwrddc01.gnilink.net...
Why should the language have a namespace feature if you are just going to
throw it away?


Why should my car have a ciggarette lighter "feature" if I'm not gonna use
it ;)

that's just me playing the devil's advocate by the way. I think "using"
using is probably not the way to go, although laziness more often than not
wins me over.


Why should my car not have a cigarette lighter just because you're
not going to use it <g>

Please don't put "using namespace whatever" in a header
file that someone else may be including. This is nearly
as bad as MS's propensity for global macro names.
Note also that std:: is not the only possible namespace.

Aside from that, it's probably largely a matter of preference. My
preference is to use the namespace prefix.
Jul 22 '05 #6
"Dan Moos" <da******@verizon.net> wrote in message
news:ZJ******************@nwrddc01.gnilink.net...
Why should the language have a namespace feature if you are just going to throw it away?


Why should my car have a ciggarette lighter "feature" if I'm not gonna use
it ;)


It's not a cigarette lighter, it's a utility power source. Some people use
it to power a cigarette lighter, but others don't.
--
Gary
Jul 22 '05 #7
cppaddict wrote:
I'm curious about the pros and cons of using the std:: prefix versus
the "using namespace std;" directive.

Obviously the directive saves you some typing, but I seem to remember
reading somewhere that it is not good form. Maybe because it is not
immediatly apparent which objects are part of the standard libraries
and which are your own.

Does anyone have additional thoughts on the matter or know what the
expert consensus is, assuming there is one?


The "expert consensus" is that using directives are bad. I think
there's a lot of misunderstanding about them, though, and while I don't
always use them, I think they are undervalued.

Using directives do not introduce anything into the current namespace.
They only make things in other namespaces available, if no local version
has been defined. This is very similar to the way that variable
declarations in an inner scope mask similar declarations in outer scopes.

A line like "using namespace defaults;" tells the compiler, "if I use a
function named foo, and there's a locally declared foo, use it, but if
there's no locally declared version, here's a good place to look for a
default." This and Koenig lookup can help make sure you always get the
best (most specialized) default versions of things. If there's ever a
conflict between Koenig and a using directive, the compiler will let you
know, and you just add a using declaration to remove the ambiguity.
This is similar to the way such a declaration can be provided in a
subclass to disambiguate use of an identifier declared in multiple base
classes. If you explicitly use std::some_function all the time, you'll
miss out on any local overloads of some_function.

#include <iostream>

namespace udts
{
struct Udt { };

void print( Udt ) { std::cout << "udts::print\n"; }
}

namespace defaults
{
void foo ( ) { std::cout << "defaults::foo\n"; }
void bar ( ) { std::cout << "defaults::bar\n"; }

void print( udts::Udt ) { std::cout << "defaults::print\n"; }
}

namespace local
{
void bar ( ) { std::cout << "local::bar\n"; }

void f ( )
{
using namespace defaults;

bar( ); // Local bar.
foo( ); // Defaults to defaults::foo.

using udts::print;
// The using directive conflicts with Koenig, so the
// compiler will give an error about ambiguity if this
// declaration is not provided.

print( udts::Udt( ) ); } }
int main ( )
{
local::f ( );
}
Jul 22 '05 #8

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

Similar topics

8
by: Boris | last post by:
Is it possible to manipulate the std::ostream to prepend a string when performing output, e.g. // manipute std::cout to prepend "prefix " std::cout << "hallo" << std::endl; // results in...
11
by: cppaddict | last post by:
Hi, I recently started using typedefs and am curious if they are ever considered evil. In particular, I'm thinking of a situation where a typedef is used in a file but defined in another,...
15
by: drdoubt | last post by:
using namespace std In my C++ program, even after applying , I need to use the std namespace with the scope resolution operator, like, std::cout, std::vector. This I found a little bit...
6
by: Peter O'Reilly | last post by:
The news of Nick Berg is shocking enough. Having his video released and made widely available, in its _totality, including the decapitation, makes those web site operators accomplices in this...
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" <<...
3
by: Schizoid Man | last post by:
Hi, I'm a novice whose just about migrating to C++ after cutting my teeth on C for a few years. To start with I'm using Microsoft's out-of-the-box Visual C++ Express Edition compiler and I...
10
by: Immortalist | last post by:
Various aquisition devices that guide learning along particular pathways towards human biases. And as E.O. Wilson might say mental development appears to be genetically constrained. (1) Language...
4
by: gallows | last post by:
I've tried to use C qsort() on an object derivate by std::vector, but it doesn't work. I've the follow structure: struct Item { std::string name; int number; }
58
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.