Using the std namespace

I am in the programming class at school, and I wanted to start doing C ++ programming outside the classroom. My school has been using Microsoft Visual C ++ 6.0 (since 1998), so it still uses <iostream.h> , not <iostream> and using namespace std . When I started working, I could not understand how and when to use using namespace std , and when you just need to use things like std::cout<<"Hello World!"<<'\n'; (for example), as well as restrictions and other uses for the namespace keyword. In particular, if I want to create a program with iostream and iomanip, should I specify “use the std namespace” twice, is there something else that I would need to use, or I can just do the same thing as I did with iostream? I tried to walk, but I didn’t understand anything. Thank you in advance.

+2
source share
5 answers

Well, there are a few things, but it is manageable.

First of all, the difference between:

 using namespace std; ... cout << "Something" << endl; 

And using

 std::cout << "Something" << std::endl; 

This is just a matter of volume. The scope is just a fancy way of saying how the compiler recognizes the names of variables and functions, among other things. The namespace does nothing more than add an extra level of scope for all variables in that namespace. When you enter using namespace std , you take everything inside the std and move it to the global scope so that you can use the shorter cout instead of the more complete std::cout .

One thing to understand the namespace is that they stretch between files. Both <iostream> and <iomanip> use the std . Therefore, if you include both, the declaration using namespace std will work in both files, and all characters in both files will be moved to the global area of ​​your program (or the area of ​​functions, if you used it inside a function).

There will be people who tell you: "Do not use using namespace std !!!!", but they rarely tell you why. Suppose I have the following program, where all I am trying to do is determine two integers and print them:

 #include <iostream> using namespace std; int main(int argc, char** argv) { int cout = 0; int endl = 1; cout << cout << endl << endl; // The compiler WILL freak out at this :) return 0; } 

When I use using namespace std , I open the door for naming collisions. If I (randomly) name the variable the same as in the header, then your program will break and it will be difficult for you to determine why.

I can write the same program as before (but make it work) without using the using namespace std statement:

 #include <iostream> int main(int argc, char** argv) { int cout = 0; int endl = 1; std::cout << cout << endl << std::endl; // Compiler is happy, so I'm happy :) return 0; } 

Hope this clarified some things.

+11
source

If you use header names without .h , then the elements declared / defined in it will be in the std . You need to use only using namespace std; once in the area where you want to import things to get everything; more than one using namespace std; nothing helps.

I would recommend against using namespace std; at all. Instead, I prefer to say, for example, using std::cout; to save names in std from conflict with mine.

For instance:

 #include <iostream> #include <iomanip> int main() { using namespace std; int left = 1, right = 2; cout << left << " to " << right << "\n"; } 

can cause mysterious problems because left and right exist in the std (like IO-manipulators), and they are imported if you lazily say using namespace std; . If you want to use IO manipulators instead of outputting variables, you may be a little disappointed. But the intention is not obvious in any case. Perhaps you just forgot that you have ints named left and right .

Instead, if you say

 #include <iostream> #include <iomanip> int main() { using std::cout; int left = 1, right = 2; cout << left << " to " << right << "\n"; } 

or

 #include <iostream> #include <iomanip> int main() { int left = 1, right = 2; std::cout << left << " to " << right << "\n"; } 

everything works as expected. In addition, you can see what you are actually using (which in this case does not contain anything from <iomanip> ), so it’s easier for you to reduce your needs to what you need.

+4
source

Here 's a good link that describes namespaces and how they work.

Both methods are correct, that is, you can either enter a namespace using the using statement, or you can qualify all members of the namespace. This is a coding style issue. I prefer qualification with namespaces because it makes it clear to the reader in which the namespace is defined by a function / class.

In addition, you do not need to enter a namespace twice if you include multiple files. One operator is enough.

+1
source

Specifically for using namespace std

You really should never use it in a header file. So you imported the whole "std" into the global namespace for anyone who includes your header file, or for anyone else who includes a file that includes your file.

Using it inside a .cpp file is a personal preference. I usually don’t import the entire std into the global namespace, but there seems to be no harm to this to save a bit of input.

+1
source
Good question, Ryan. What using namespace does, imports all the characters from a particular namespace (scope) into the area in which it was used. For example, you can do the following:
 namespace A { struct foo {}; } namespace B { using namespace A; struct bar : foo {}; } 

In the above examples, all characters in the namespace A become visible in the namespace B , as if they were declared there.

This import only affects this translation unit. So, for example, when in your implementation file (i.e. .cpp ) you execute using namespace std; , you basically import all the characters from the std into the global scope.

You can also import certain characters, but not all, for example:

 using std::cout; using std::endl; 

You can do this in the global scope of the namespace or in the scope of functions, for example:

 int main () { using namespace std; } 

The programmer must decide when to use fully qualified names and when to use the using keyword. It is usually very bad to insert using in header files. Professional C ++ programmers almost never do this unless it is required to solve a problem, or they are 100% sure that this will not spoil the type resolution for those who use this header.

However, in the source file (none of them contain source files), it's ok to do any use statements if there are no conflicting names in different namespaces. This is just a matter of taste. For example, if there are a lot of characters in different code from different namespaces, I would prefer at least some hints as to where they are declared. But everyone is familiar with STL, so using namespace std; should never hurt.

There may also be several long namespaces, and in these cases it is convenient to use namespace aliases. For example, there is a Boost.Filesystem library that puts all of its characters in the boost::filesystem namespace. Using this namespace will be too big, so people usually do something like this:

 namespace fs = boost::filesystem; fs::foo (); fs::bar (); 

Also, it is almost normal to use namespace aliases in headers, for example:

 namespace MyLib { namespace fs = boost::filesystem; } 

.. and benefit from less typing. It happens that users who use this header will not import the entire file system library by saying using namespace MyLib; . But then they will import the "fs" namespace from your library, which may conflict with something else. So it's best not to do this, but if you want it too badly, it's better to say using namespace boost::filesystem there.

So, back to your question. If you are writing a library using C ++ I / O streams, it is better not to have any using statements in the headers, and I would go with using namespace std; in every cpp file. For instance:

somefile.hpp:

 namespace mylib { class myfile : public std::fstream { public: myfile (const char *path); // ... }; } 

somefile.cpp:

 #include "somefile.hpp" using namespace std; using namespace mylib; myfile::myfile (const char *path) : fstream (path) { // ... } 
+1
source

Source: https://habr.com/ru/post/1412576/


All Articles