Don't let the C + + header file appear with the "using namespace xxx;" __c++

Source: Internet
Author: User
Here, I say this without a word:
Reference I never want to see "using namespace xxx" in any header file. Had
As a developer/team leader, I often recruit new project members and sometimes help other groups to interview candidates. As one of the recruitment processes, I often ask candidates to write some code, so I've examined quite a bit of code. In the recently submitted C + + code, I noticed a trend that in any header file, I always see the following code:

C + + code using namespace std;

If I test the code with our code-checking system (which I highly recommend in practice), the line of code is often followed by a comment "Timo will not write." They're right, I don't really write that.

So why did I persuade a lot of C + + textbooks (perhaps not a good thing) to make them think that using the above code was a very bad way.

Let's take a look at what the above code does. In general, it introduces all content within the namespace "Std" (or other author-invoked namespaces) without exception to the current namespace. Please note that I say "all content", not one or two you want to use the class \ type \ template. The reason for introducing namespaces at the beginning of a piece of code is to enhance the program modularity and reduce naming conflicts. In general, it allows you to write code similar to the following, and ensure that the compiler can choose the correct implementation:

C + + code std::vector<std::string> names; My_cool_reimplementation::vector<our_internal_stuff::string> Othernames;

Now, suppose we're trying to reduce code entry and use a using declaration in the above code (or, worse, two namespaces are declared), implemented as follows:

C + + code vector<string> names; Vector<our_internal_stuff::string> Othernames;

If the author of the Code is lucky, the compiler chooses the correct implementation of the vector, or at least at the initial stage. But after a while, you'll run into some very strange compiler errors. With luck, you can find the cause of these mistakes--I've had a similar problem, and it took me days to find out why. Damn, they'll waste you a lot of time just because you want to code less than 5 characters.

And, if you use the using declaration in the header file, you will make this kind of problem worse, because the naming conflict problem will sooner or later in a call very very far module in the presence of the unknown, and you may need to check the three-level call to find out why, A header file that contains another header file that directly uses a using declaration can cause the namespace to be immediately contaminated, and any file using the namespace will cause such problems if the contents of the Std namespace are used.

So why do you see in many textbooks that they use a using namespace std? My theory is that it does help to improve the layout of the entire book and reduce some of the visual clutter. In a paper book, you have only a limited space to write text, so you have to make the most of it, and the code examples in the book are usually simple. On the other hand, different namespace qualifiers bring a lot of visual clutter, which makes it difficult for the reader to judge the author's intentions from the context. When you want to write some efficient code in this era, the above two points are not completely right, now the compiler most can handle 60-80 words per line (you can try, this can be). Therefore, do not randomly introduce namespaces.

If you very clearly want to use a using declaration in a header file, what should you do? There are other ways we can reduce the case that we have to use a using declaration-you can use one of the following, or a combination of many ways.

First, you just use a typedef. I would suggest that you use this method, even though I don't always follow my own advice, and I think it's a good way to do it in practice anyway. In fact, there are two advantages to using a typedef--he makes a type name more readable, and if you choose a good name, it can make the author's intentions more obvious. Compare the following declaration methods:

C + + code std::map<std::string, long> clientlocations;   typedef std::map<std::string, long> Clientnametozip; Clientnametozip clientlocations;

The second declaration-even if it is expanded to two lines-is more intuitive than the first declaration, and it avoids the namespace ambiguity.

Another option is to limit the scope of a using declaration in two ways-just the "using" symbol you want to use, for example:

C + + code using Std::string;

However, throwing this statement into the header file is almost as bad as using a "using namespace", so you should use the scope to limit its visibility to ensure that your using declaration is really only valid for the first time using the declaration. For example, you can limit the scope of a class declaration by using the following methods:

C + + code namespace bar {struct zzz {...};     class Foo {using namespace bar; ZZZ M_snooze; Pulls in bar::zzz};

Alternatively, you can limit the scope of the using to a function, for example:

C + + code void Temp () {using namespace std;   String test = "FooBar"; }

Either way, you can limit the scope of the using to the code that needs it, rather than putting it in the public space of the code. The bigger your project, the more important it is to ensure modularity and minimize unpredictable negative effects.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.