Is there a way to reference cout without using namespace std or prefixing with std::?
Asked Answered
F

6

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.

Fredric answered 7/8, 2018 at 8:50 Comment(5)
Prefer the use of std::cout.Steerageway
Pardon me, but why?Fredric
Because it is the best practice in C++ and introducing the entire std namespace is bad practice.Steerageway
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.Fredric
@Steerageway 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).Nuzzle
S
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.

Stokehole answered 7/8, 2018 at 8:53 Comment(4)
Thank you, I think that's exactly what I was looking for!Fredric
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?Fredric
@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.Stokehole
@ThomasKowalski: Might be useful to mention #include <iosfwd> here. If you jus need the std::ostream declaration, you don't need all of <ostream>Dependent
N
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.

Nummular answered 7/8, 2018 at 8:54 Comment(0)
I
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)

Indevout answered 7/8, 2018 at 8:55 Comment(0)
T
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

Titania answered 7/8, 2018 at 8:56 Comment(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 workNummular
D
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.";
}
Dedededen answered 7/8, 2018 at 9:2 Comment(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 aliasNummular
S
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();
}
Sarong answered 7/8, 2018 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.