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.

Thomas Kowalski
  • 1,995
  • 4
  • 20
  • 42
  • 3
    Prefer the use of `std::cout`. – Ron Aug 07 '18 at 08:53
  • Pardon me, but why? – Thomas Kowalski Aug 07 '18 at 08:54
  • Because it is the best practice in C++ and introducing the entire `std` namespace is [bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Ron Aug 07 '18 at 08: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. – Thomas Kowalski Aug 07 '18 at 08: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 07 '18 at 08:59

6 Answers6

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thank you, I think that's exactly what I was looking for! – Thomas Kowalski Aug 07 '18 at 08: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`? – Thomas Kowalski Aug 07 '18 at 08: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. – juanchopanza Aug 07 '18 at 09:02
  • 1
    @ThomasKowalski: Might be useful to mention `#include ` here. If you jus need the `std::ostream` declaration, you don't need all of `` – MSalters Aug 07 '18 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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
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)

pablo285
  • 2,460
  • 4
  • 14
  • 38
Jarod42
  • 203,559
  • 14
  • 181
  • 302
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

Binpord
  • 90
  • 8
  • 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 – 463035818_is_not_an_ai Aug 07 '18 at 09: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.";
}
joe_chip
  • 2,468
  • 1
  • 12
  • 23
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();
}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142