2

I am a python programmer, and just learning some C++ on the side.

From what I understand, in C++ using namespace std
would be equivalent to python's from std import *, and should not be used.

1) Correct?

And then in Python, I could do for example from std import cout, cin.

2) Is there a single line equivalence? Or would I have to do it in multiple lines?

using std::cout;
using std::cin;
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Roman
  • 8,826
  • 10
  • 63
  • 103
  • To answer your first question: [Why is “using namespace std;” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – BlackDwarf Jun 17 '15 at 12:19
  • @DeepBlackDwarf: Thanks. Though I am aware of the bad practice from python. – Roman Jun 17 '15 at 12:21
  • 3
    It's more idiomatic to simply use `std::cout` and `std::cin`. – Shoe Jun 17 '15 at 12:23
  • If you preface everything you can get burned if two different namespaces use the same name. You can also use a [namespace alias](http://stackoverflow.com/questions/1211399/in-c-what-is-a-namespace-alias) to shorten long namespace names. – NathanOliver Jun 17 '15 at 12:28
  • Since statements in C++ end with a ";", you can put multiple statements on the same line. Though I don't guarantee that this would be any more readable – KABoissonneault Jun 17 '15 at 12:29

2 Answers2

3

Shortly:

  1. Correct.
  2. There is no any single line equivalent for this.
VP.
  • 15,509
  • 17
  • 91
  • 161
1

Yes , there is one way you can import multiple members but its not supported everywhere.

using std::cout,std::endl;

But you will get a warning.

warning: comma-separated list in using-declaration only available with -std=c++1z or -std=gnu++1z using std::cout,std::endl; ^