Board index » cppbuilder » std::<io> in main()

std::<io> in main()


2007-04-11 12:38:48 AM
cppbuilder112
Is it [better/best/irrelevant] to put the following [BEFORE] main (and maybe
before prototypes), but [AFTER] the includes instead of [IN] main:
using std::cout;
using std::cin;
using std::endl;
The text book I'm using has:
int main()
{
using std::cout;
using std::cin;
using std::endl;
...
I'm trying to develop good habits for formatting source code.
It appears that includes go first...
then prototypes...
then main()...
then functions()...
Thanks, Tracey
 
 

Re:std::<io> in main()

"Tracey" < XXXX@XXXXX.COM >writes:
Quote
Is it [better/best/irrelevant] to put the following [BEFORE] main (and maybe
before prototypes), but [AFTER] the includes instead of [IN] main:

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

The text book I'm using has:
int main()
{
using std::cout;
using std::cin;
using std::endl;
...
The difference is just the scope of the using directive. At file
scope, everything in the file is "using" std::cout (and others), but
when it's in main, only main is using the name, and code outside of
main must still qualify the names.
You may think that it's better to use the names at file scope, because
more code (the whole file) can take advantage of it. Others might
argue that it's worse for the same reason.
The entire point of namespaces in C++ is to partition the names into a
hierarchy, to avoid collisions and conflicts. When you use a
namespace, or use names from a namespace, you're flattening the
hierarchy, and increasing the chance of there being a naming
collision. It is usually not a problem, but when it actually is a
problem, it can be incredibly subtle--such that sometimes you might
not even notice you have a problem, but worse, you have a problem and
don't know what it is.
I like to restrict the scope of using directives to the smallest scope
possible for them to still be valuable, if I use them at all. (It is
sometimes very convenient, but never necessary.)
--
Chris (TeamB);
 

Re:std::<io> in main()

What is "better" is a value judgement. If asking five people, expect six
answers, not all in agreement.
If using an item inside a function and wishing to skip decorating it with
std:: then put the using statement inside the function, preferably at or
near the beginning of the function.
If using such an item throughout most functions in a source file then put
the using statement for that item after the includes and after any #pragma
hdrstop.
Quote
I'm trying to develop good habits for formatting source code.
It appears that includes go first...
then prototypes...
then main()...
then functions()...
I start with includes then functions. If there is to be one in this file, I
put main or WinMain last. I rarely use a prototype in the file where that
same function is located because I put a function in a source file before it
is used.
. Ed
Quote
Tracey wrote in message
news:461baf89$ XXXX@XXXXX.COM ...

Is it [better/best/irrelevant] to put the following [BEFORE] main
(and maybe before prototypes), but [AFTER] the includes
instead of [IN] main:

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

The text book I'm using has:
int main()
{
using std::cout;
using std::cin;
using std::endl;
...

I'm trying to develop good habits for formatting source code.
It appears that includes go first...
then prototypes...
then main()...
then functions()...
 

{smallsort}

Re:std::<io> in main()

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >writes:
Quote
"Tracey" < XXXX@XXXXX.COM >writes:

>Is it [better/best/irrelevant] to put the following [BEFORE] main (and maybe
>before prototypes), but [AFTER] the includes instead of [IN] main:
>
>using std::cout;
>using std::cin;
>using std::endl;
>
>The text book I'm using has:
>int main()
>{
>using std::cout;
>using std::cin;
>using std::endl;
>...

The difference is just the scope of the using directive.
[picking nit] These are using *declarations*.
 

Re:std::<io> in main()

XXXX@XXXXX.COM (Thomas Maeder [TeamB]) writes:
Quote
>The difference is just the scope of the using directive.

[picking nit] These are using *declarations*.
Thanks.
using std::cout; // declaration
using namespace std; // directive
--
Chris (TeamB);