Re: cout vs std::cout

Discussion in 'C++' started by Stefan Ram, Sep 28, 2008.

  1. Stefan Ram

    Stefan Ram Guest

    Mark Casternoff <> writes:
    >1) using std::cout;
    > cout << "This is a test";
    >2) std::cout << "This is a test";


    I believe the majority of programmers is using style 1.

    I prefer style 2, precisely, I prefer:

    ::std::cout << "alpha";

    Reasons:

    This code segment has a context-independent meaning
    to the maximum extend possible by such a segment:

    It can be copied from one source file to another
    without the need to copy a using declaration, too.

    It can even be copied into

    namespace example { namespace std {} void example(){ ... }}

    and still retain its meaning.

    ISO/IEC 14882:2003(E) is using »::std« once,
    »static ::std::locale::id« in 22.1.1.1.2.p1.
     
    Stefan Ram, Sep 28, 2008
    #1
    1. Advertising

  2. Stefan Ram

    Rolf Magnus Guest

    Stefan Ram wrote:

    > Mark Casternoff <> writes:
    >>1) using std::cout;
    >> cout << "This is a test";
    >>2) std::cout << "This is a test";

    >
    > I believe the majority of programmers is using style 1.
    >
    > I prefer style 2, precisely, I prefer:
    >
    > ::std::cout << "alpha";
    >
    > Reasons:
    >
    > This code segment has a context-independent meaning
    > to the maximum extend possible by such a segment:
    >
    > It can be copied from one source file to another
    > without the need to copy a using declaration, too.


    Well, you need to copy the #includes too.

    > It can even be copied into
    >
    > namespace example { namespace std {} void example(){ ... }}
    >
    > and still retain its meaning.


    I've never seen that anyone added his own namespace std somewhere. It would
    be quite odd, so I don't see why I should attempt to protect against such a
    case.

    > ISO/IEC 14882:2003(E) is using »::std« once,
    > »static ::std::locale::id« in 22.1.1.1.2.p1.


    Once isn't really that often. How often does it not use that?

    A disadvantage of explicitly qualifying with ::std or even with std is that
    it makes it hard to create your own cout and replace all uses of the
    standard cout with this one. If you have a

    using std::cout;

    cout << "Hello world\n";

    you can simply replace the using declaration and you're done instead of
    changing every single occurance of cout in your code.
    And yes, I actually had a case where I wanted to exchange std::cout with my
    own one. It was a microcontroller device with quite limited resources, so I
    wanted to have something that behaves similar to cout, but provides only a
    limited subset of the functionality to save resources.
     
    Rolf Magnus, Sep 29, 2008
    #2
    1. Advertising

  3. Rolf Magnus wrote:
    > [...]
    > A disadvantage of explicitly qualifying with ::std or even with std is that
    > it makes it hard to create your own cout and replace all uses of the
    > standard cout with this one. If you have a
    >
    > using std::cout;
    >
    > cout << "Hello world\n";


    What about
    std::eek:stream& mine = std::cout;
    then? This allows to redefine 'mine' later and doesn't
    need to have 'std::' removed from 'cout'. Or am I missing
    something?

    > [...]


    Schobi
     
    Hendrik Schober, Sep 29, 2008
    #3
  4. Stefan Ram

    Rolf Magnus Guest

    Hendrik Schober wrote:

    > Rolf Magnus wrote:
    >> [...]
    >> A disadvantage of explicitly qualifying with ::std or even with std is
    >> that it makes it hard to create your own cout and replace all uses of the
    >> standard cout with this one. If you have a
    >>
    >> using std::cout;
    >>
    >> cout << "Hello world\n";

    >
    > What about
    > std::eek:stream& mine = std::cout;
    > then?
    > This allows to redefine 'mine' later and doesn't
    > need to have 'std::' removed from 'cout'. Or am I missing
    > something?


    That would work, too - if you wrote your whole program with that in mind and
    only use libraries where this is done too.
     
    Rolf Magnus, Sep 29, 2008
    #4
  5. Rolf Magnus wrote:
    > Hendrik Schober wrote:
    >
    >> Rolf Magnus wrote:
    >>> [...]
    >>> A disadvantage of explicitly qualifying with ::std or even with std is
    >>> that it makes it hard to create your own cout and replace all uses of the
    >>> standard cout with this one. If you have a
    >>>
    >>> using std::cout;
    >>>
    >>> cout << "Hello world\n";

    >> What about
    >> std::eek:stream& mine = std::cout;
    >> then?
    >> This allows to redefine 'mine' later and doesn't
    >> need to have 'std::' removed from 'cout'. Or am I missing
    >> something?

    >
    > That would work, too - if you wrote your whole program with that in mind and
    > only use libraries where this is done too.


    Well, in your case you have to use libraries which
    adhere to some other convention ('using std::cout')
    as well. (To me it seems that a lib that writes to
    'std::cout' instead of a user-provided stream is in
    need of an overhaul.)

    Schobi
     
    Hendrik Schober, Sep 30, 2008
    #5
  6. Stefan Ram

    James Kanze Guest

    On Sep 29, 5:35 am, Rolf Magnus <> wrote:

    [...]
    > A disadvantage of explicitly qualifying with ::std or even
    > with std is that it makes it hard to create your own cout and
    > replace all uses of the standard cout with this one.


    I'd say just the opposite. A global search and replace on
    \<std::cout\> will work just fine. A global search and replace
    on \<cout\> will change not only the cout in std, but any other
    cout you happen to have as well. (And of course, if it's
    something special, you probably want to call it something other
    than cout anyway.)

    > If you have a


    > using std::cout;


    > cout << "Hello world\n";


    > you can simply replace the using declaration and you're done
    > instead of changing every single occurance of cout in your
    > code.


    The problem with this is that it fools the reader. If you see
    cout, and you know that the programmer often makes use of using
    declarations, you'll just assume that it's std::cout. If you
    see std::cout, you know it's std::cout, and if you see
    MyNamespace::cout, you know it isn't.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 30, 2008
    #6
  7. Stefan Ram

    James Kanze Guest

    On Sep 29, 8:36 pm, Rolf Magnus <> wrote:
    > Hendrik Schober wrote:
    > > Rolf Magnus wrote:
    > >> [...]
    > >> A disadvantage of explicitly qualifying with ::std or even
    > >> with std is that it makes it hard to create your own cout
    > >> and replace all uses of the standard cout with this one. If
    > >> you have a


    > >> using std::cout;


    > >> cout << "Hello world\n";


    > > What about
    > > std::eek:stream& mine = std::cout;
    > > then?
    > > This allows to redefine 'mine' later and doesn't
    > > need to have 'std::' removed from 'cout'. Or am I missing
    > > something?


    > That would work, too - if you wrote your whole program with
    > that in mind and only use libraries where this is done too.


    Libraries shouldn't ever write to std::cout.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 30, 2008
    #7
  8. Stefan Ram

    LR Guest

    James Kanze wrote:
    > On Sep 29, 5:35 am, Rolf Magnus <> wrote:
    >
    > [...]
    >> A disadvantage of explicitly qualifying with ::std or even
    >> with std is that it makes it hard to create your own cout and
    >> replace all uses of the standard cout with this one.

    >
    > I'd say just the opposite. A global search and replace on
    > \<std::cout\> will work just fine. A global search and replace
    > on \<cout\> will change not only the cout in std, but any other
    > cout you happen to have as well. (And of course, if it's
    > something special, you probably want to call it something other
    > than cout anyway.)
    >
    >> If you have a

    >
    >> using std::cout;

    >
    >> cout << "Hello world\n";

    >
    >> you can simply replace the using declaration and you're done
    >> instead of changing every single occurance of cout in your
    >> code.

    >
    > The problem with this is that it fools the reader. If you see
    > cout, and you know that the programmer often makes use of using
    > declarations, you'll just assume that it's std::cout. If you
    > see std::cout, you know it's std::cout, and if you see
    > MyNamespace::cout, you know it isn't.


    Sometimes, because of the environment I use I find this little snippet
    useful:

    namespace non_std { // or whatever namespace you'd like
    std::eek:stream &cout = std::cout;
    }

    And in other code...
    non_std::cout << "something" << std::endl;

    When I don't want to write to std::cout for some reason, it's pretty
    easy to change non_std::cout to refer to some other stream.

    I do have to pay attention to what non_std::cout is.

    LR
     
    LR, Sep 30, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,440
    Leor Zolman
    Jun 2, 2004
  2. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    459
    Jerry Coffin
    Oct 7, 2008
  3. Matthias Buelow

    Re: cout vs std::cout

    Matthias Buelow, Sep 28, 2008, in forum: C++
    Replies:
    10
    Views:
    619
    Hendrik Schober
    Oct 1, 2008
  4. saurabh29789

    Sending std::cout to std::cout !!

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    1
    Views:
    307
    Harald Finster
    Jun 11, 2009
  5. saurabh29789

    Sending std::cout to std::cout

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    2
    Views:
    353
    Rolf Magnus
    Jun 12, 2009
Loading...

Share This Page