简体   繁体   中英

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

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.

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.

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.

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)

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 . PS also answered at this post

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.";
}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM