std::cout vs cout

P

Pmb

I've noticed a lot of people preferring std::cout over cout. May I ask why
you prefer one over the other?

thanks

Pmb
 
V

Victor Bazarov

Pmb said:
I've noticed a lot of people preferring std::cout over cout. May I ask why
you prefer one over the other?

'cout' is half as short to type. OTOH, to use it you need to declare
the name 'std::cout' as _used_ in the current scope, which is commonly
done by means of 'using' declaration, which in itself a kind of blanket
statement. Often, you might not want to bring the name into the scope
and instead use the "full" form. You know, like calling somebody "Mr.
Firstname Lastname" instead of "sir".

I prefer using most of standard types and objects with the namespace
prefix, unless I am being lazy, in which case I usually just say 'using
namespace std;'.

Victor
 
L

Leor Zolman

'cout' is half as short to type. OTOH, to use it you need to declare
the name 'std::cout' as _used_ in the current scope, which is commonly
done by means of 'using' declaration, which in itself a kind of blanket
statement. Often, you might not want to bring the name into the scope
and instead use the "full" form. You know, like calling somebody "Mr.
Firstname Lastname" instead of "sir".

I prefer using most of standard types and objects with the namespace
prefix, unless I am being lazy, in which case I usually just say 'using
namespace std;'.

The "middle ground" is to bring the names you want into scope via using
/declarations/ rather than the directive:

using std::cout;
using std::endl;

Following the most recent discussion of the perils of the using directive,
I've edited my default testing template so that it contains the two
declarations above (bringing in the identifiers I most commonly use in my
little hack test programs), in place of my original
using namespace std;
directive at namespace scope (which, as it continuously gets pointed out,
is evil).
-leor
 

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,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top