5

I'm pretty new to C++, and I'm using std::cout for debugging purposes.

Though, I'd really like to be able to just use cout rather than the whole std::cout thing. I know i could import the std namespace, but I've been explained it was a bad thing due to name clashing that can occur because of this.

Is there anyway to do this?

I tried

std::ostream cout = std::cout;

But I get

function "std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits> &) [with _CharT=char, _Traits=std::char_traits<char>]" (declared at line 391 of "/usr/include/c++/5/ostream") cannot be referenced -- it is a deleted function

Please suggest.

5
  • 3
    Prefer the use of std::cout.
    – Ron
    Aug 7, 2018 at 8:53
  • Pardon me, but why? Aug 7, 2018 at 8:54
  • Because it is the best practice in C++ and introducing the entire std namespace is bad practice.
    – Ron
    Aug 7, 2018 at 8:55
  • Yes but I actually wanted just to introduce some elements of the namespace (cout, endl, that kind of things), as I stated in my post. Aug 7, 2018 at 8:57
  • 1
    @Ron What is considered a bad practice is unscoped using declarations. Having using std; or using std::cout; inside a function or a block in general does not have anything bad. And depending on the case, some may argue that even unscoped using is fine in a .cc/.cpp file, since it does not affect other compilation units (although I do prefer to avoid that too personally).
    – jdehesa
    Aug 7, 2018 at 8:59

6 Answers 6

15

Sure, with a using declaration:

using std::cout;

Usual health warnings about not doing this in header files, and limiting it to small scopes apply.

4
  • Thank you, I think that's exactly what I was looking for! Aug 7, 2018 at 8:55
  • Also, what would be the recommended way to "import" the ostream type? Should I rather use a typedef std::ostream ostream or rather using std::ostream? Aug 7, 2018 at 8:56
  • 1
    @ThomasKowalski You can't "import" things in C++, at least not yet. What you need to do is include the appropriate header. I would probably use the using declaration as shown above, but only in an implementation file, and in a limited scope. But in general, I just write std:: most of the time. Aug 7, 2018 at 9:02
  • 1
    @ThomasKowalski: Might be useful to mention #include <iosfwd> here. If you jus need the std::ostream declaration, you don't need all of <ostream>
    – MSalters
    Aug 7, 2018 at 10:25
7

You can't copy streams (think about it, it just doesn't make sense), but you can get a reference to them:

std::ostream& my_cout = std::cout;

However, I would strongly advice you not to do so. If you see in some code std::cout you can be almost 100% certain that you know what it is. On the other hand a cout alone you should already look suspicious to you and a my_cout could really be anything. I know it is hard, but get used to type std::, on the long run it will help you more than you need time to type those 5 letters.

4

Your way would be:

auto& cout = std::cout;

but you might simply do

using std::cout;

(with similar restriction than using namespace: not in namespace scope in header, ideally limiting the scope of using directive as much as possible)

2

Sorry, too low rating to comment, but why don't you just type

using std::cout;

at the top of the file and then just use cout. P.S. also answered at this post

1
  • 1
    irrespective your rep, this should be an answer not a comment. Asnwering in comments is tempting, but it bypasses reviews and votes, which are essential to make SO work Aug 7, 2018 at 9:00
0

One way is using-declaration, which introduces only std::cout instead of all names in std:::

using std::cout;

The way you tried won't work - it's an attempt to copy std::cout to another object - and std::cout is not copyable.

Alternative is to use a reference:

std::ostream& cout = std::cout;

Now, cout points to std::cout instead of being its copy.

The second way can be useful, if you want to, for example, write a function without deciding whether it should output to cout, file or something else:

void func(std::ostream& output) {
    output << "works with cout, files, etc.";
}
1
  • minor nitpick: cout doesnt really "point" to std::cout. imho it is more correct to say cout is std::cout, because a reference is just an alias Aug 7, 2018 at 9:08
0

If you're looking to abbreviate std::cout, it could be that what you are really looking for is dependency injection.

Remember that std::cout is a reference to a model of a std::ostream.

We can use that in our favour to make code more re-usable, testable and loosely coupled.

example:

#include <iostream>
#include <sstream>

std::ostream& do_something(std::ostream& os)
{
    os << "Hello, World!\n";
    return os;
}


int main()
{
    // inject std::cout
    do_something(std::cout);

    // inject a stringstream
    std::ostringstream ss;
    do_something(ss);
    std::cout << ss.str();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.