Style and Namespaces

Possible duplicate:
Why is 'namespace std;' used considered bad practice in C ++?

I saw code examples in which people use, say, std::cout , while in other places people will use using namespace std; top for simplicity. Which is usually preferable?

+6
source share
3 answers

Use std::cout to avoid potential name conflicts. If you are using namespace std; , you populate your global namespace with the entire std name, which may conflict with class or function names that you or someone else in your team wrote. This is well explained in C ++ faq lite and in fooobar.com/questions/618 / ...

+3
source

I personally use the full namespace name when writing code, for example. std::string etc. This makes it more clear for those who read this what function the developer wants to use.

I saw the following statement:

write it once, read it a thousand times ...

:)

+2
source

First of all, note that you should never use using namespace in the header - you may already know about this. The reason is that it will result in using in any source files that include it.

Even at the source file level, I prefer to explicitly qualify the standard library functions and classes liek std::cout . However, in some cases, I will use the specific use instead for convenience (e.g. using std:endl ). However, I prefer only explicit qualifications.

0
source

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


All Articles