What is using namespace?

6

Since the beginning of my C ++ learning, I've been told to use namespace std , so I do not spend all the time using std::cout .

However, after studying more about language, I learned that :: is a scope operator, but what about the namespace , what behaviors and influences it in code?

    
asked by anonymous 05.12.2018 / 15:38

1 answer

8

It's a language directive that dictates that you use a namespace implicitly in that code. This command actually prevents you from naming namespace .

The namespace is an over name for types, functions, and other possible members. It is a way of avoiding equal names by making different things collide and. Then everything you have to use has to write the surname followed by :: and there the name of what it will use. This is just not necessary for what is in the global namespace .

Since most cases do not give a problem and do not cause ambiguity you can avoid typing this last name in each call, you say that starting from that point in the code until the end of the scope you accept that surname is already valid. >

There are people who say that it is not good to do this, but it is an exaggeration. Read more about Why is it not good practice to use std namespace in C ++? .

If you want to understand more about namespaces has on C # , the concept is identical .

You may be wondering if you could not just write using . You can not because alone it does something else, it works as a alias for types and other language constructs or specific statements .

    
05.12.2018 / 15:47