Using C ++ for use in std and boost

Possible duplicate:
Do you prefer explicit namespaces or "use" in C ++?

Hi guys, I am a C # developer, but my friend is C ++, and he showed me code that is populated with calls like std :: for_each and boost :: bind that were used for use in C #, and thought that using the directive will swing for readability of the code and, as a rule, faster development, it would be a pain in the neck to type any namespace before the approval of C # foreach, for example.

What are the downsides and pros of using these popular namespaces? Is it better to include these namespaces or not?

+3
source share
2 answers

First of all, let’s say two differences:

1) There are directives like using namespace std;using declarations likeusing std::cout;

2) You can put using directives and declarations in the header (.h) or the implementation file (.cpp)

In addition, the use of directives and declarations brings names into the namespace in which they are written, i.e.

namespace blah
{
    using namespace std; // doesn't pollute the *global* namespace, only blah
}

Now, from a best practice point of view, it is clear that using directives and declarations in the header file in the global namespace is a terrible no-no. People will hate you for this because any file that includes this header will be polluted by the global namespace.

, . , . , , ( ).

, , , "" , .

namespace MyLocalName
{
    using namespace boost;

    class X
    {
        // can use things from boost:: here without qualification
    };
}

using MyLocalName::X; // brings X back into the global namespace

using namespace boost; -, Boost .

+9

using namespace, , , . , , 500 loc .

0

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


All Articles