5

I'm trying to use shorter syntax and avoid using std:: everywhere, so I started using new alias syntax. In some examples I saw people using it this way:

using json = nlohmann::json;

and tried this with std::, but with code below:

#include <iostream>

using cout = std::cout;

int main()
{
    cout << "Sometext";
    return 0;
}

but I get error 'cout' in namespace 'std' does not name a type. I know I can use

using std::cout;

but why using cout = std::cout; doesn't work?

EDIT:

To all that votes to close this question: I posted it, because I wasn't able to find a solution, by writing by error message. Yes, question mentioned as one that has solution for my problem describes what happens, but when someone gets this sort of error, he will not find a solution easily. I just didn't realize, that cout is an object. I've read some questions like that, but still had no clue what happens.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Staszek
  • 849
  • 11
  • 28
  • 1
    Downvotes without comments are against new rules, that are described here: https://stackoverflow.blog/2018/04/26/stack-overflow-isnt-very-welcoming-its-time-for-that-to-change/ so again, please comment on that. I would really like to make it better. – Staszek Jun 22 '18 at 08:19
  • `using` declares types and type aliases. `std::cout` is an object, not a type, so `using` can't create an alias for it. – Peter Jun 22 '18 at 08:32
  • 3
    @Staszek Also, no new *rules* were described in that blog post, people are still free to downvote as they wish – Nick is tired Jun 22 '18 at 08:37
  • 1
    @Staszek that's a blog post, not new rules (and barely even qualifies as a new rule proposal) – M.M Jun 22 '18 at 08:44
  • Ok, it's not new rules. Still in this blog post author encourage people to be nicer. Isn't it against "be nice" policy to downvote question without stating reason, especially when it is not obvious? – Staszek Jun 22 '18 at 08:47
  • 2
    @Staszek No, it's not, the be nice policy includes 1) Rudeness and belittling language are not okay. 2) Be welcoming, be patient, and assume good intentions. 3) Don't be a jerk. That blog post has also got a fairly bad [rep from a lot of people](https://meta.stackoverflow.com/questions/366858/when-is-stack-overflow-going-to-stop-demonizing-the-quality-concerned-users-who), you won't necessarily get much support for it – Nick is tired Jun 22 '18 at 08:49
  • 4
    Possible duplicate of [What is the logic behind the "using" keyword in C++?](https://stackoverflow.com/questions/20790932/what-is-the-logic-behind-the-using-keyword-in-c) – Joseph D. Jun 22 '18 at 10:19

1 Answers1

18

using cout = std::cout; refers to type alias declaration syntax. It's similar to typedef; so you're trying to declare a type named cout that refers to a previously defined type std::cout. But std::cout is not a type name, it's an object with type of std::ostream.

As the error message said, it's just trying to tell you that std::cout doesn't refer to a type name.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 2
    The effect of `static auto& cout = std::cout;` is nearly indistinguishable from what OP tried, and would allow a different name. – Deduplicator Jun 22 '18 at 08:39