Valid STL Clause 48

Source: Internet
Author: User
Clause 48: always # include the appropriate header file

One of the secondary concerns of STL programming is that, although it is easy to build software that can be compiled on one platform, # include instructions must be attached on other platforms. This annoyance comes from the fact that the C ++ standard (unlike the c Standard) fails to specify which standard header file must or may be # include by another standard header file. With this flexibility, different implementations will choose to do different things.

What does this mean in practice? I can give you some concepts. I used five STL platforms (we call them A, B, C, D, and E ), it took some time to test some small programs on them to see which standard header file I can ignore and still compile successfully. This grounding tells me which header file # includes others. This is what I found:

  • For A and C, <vector> # des <string>.
  • For C, <algorithm> # des <string>.
  • For C and D, <iostream> # includes <iterator>.
  • For D, <iostream> # des <string> and <vector>.
  • For D and E, <string> # des <algorithm>.
  • For all the five implementations, <set> # includes <functional>.

Except for <set> # include <functional>, I cannot implement B by using programs that lack header files. According to the Murphy's Law, you will always develop on platforms like a, c, d, or E, and then transplant them to platforms like B, especially when the transplantation is under great pressure and the completion time is very tight.If there are two or more ways to do something, and one of those ways can result in a catastrophe, then someone will do it. (When there are two or more ways for you to choose, if one of them will lead to failure, you will definitely choose it ). After many years, this "law" has gradually entered the scope of idioms, and its connotation has been endowed with Infinite Creativity. many variants have emerged, the most famous one is also known as finagle's law. The specific content is: If anything can go wrong, it will. (errors will occur, and errors will eventually occur .). This law is regarded as the best imitation and elaboration of Murphy's Law .)

But do not blame the compiler or library implementation you are about to transplant. If you lack the required header file, this is your fault. Whenever you reference the elements in the STD namespace, you should be responsible for the # include header file. If you miss them, your code may also be compiled, but you still lack the necessary header files, and other STL platforms may resist your code.

To help you remember what you need, there is a quick summary of what is in each standard STL-related header file:

  • Almost all containers are in header files with the same name. For example, vector declares in <vector> and list declares in <list>. The exception is <set> and <map>. <Set> set and Multiset are declared. <map> map and multimap are declared.
  • All except the four algorithms are declared in <algorithm>. The exception is accumulate (see clause 37), inner_product, adjacent_difference, and partial_sum. These algorithms are declared in <numeric>.
  • Special iterators, including istream_iterators and istreambuf_iterators (see section 29), are declared in <iterator>.
  • Standard functions (such as less <t>) and function adapters (such as not1 and bind2nd) are declared in <functional>.

Whenever you use any component in a header file, you must be sure to provide the corresponding # include indication, even if your development platform allows you to compile without it. When you find that porting to a different platform reduces the stress, your diligence will be rewarded.

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.