I was just checking an answer and realized that CHAR_BIT isn’t defined by headers as I’d expect, not even by #include <bitset>, on newer GCC.
Do I really have to #include <climits> just to get the “functionality” of CHAR_BIT?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As you may know, whether or not an implementation wants to include other headers is unspecified. It’s allowed, but not mandated. (§17.4.4.1) So you either have to be explicit or know your guarantees.
The only time a C++ header must include another is if it requires a definition in another. For example,
<bitset>is required to include<cstddef>forstd::size_t, as this is explicitly stated in the standard. (§23.3.5, for this example)For a counter-example, consider
<limits>. It may include<climits>and define the values fornumeric_limitsin terms of the macros within, and it often does since that’s easiest for an implementation. But all the standard says is things like: “Equivalent toCHAR_MIN,SHRT_MIN,FLT_MIN,DBL_MIN, etc.” but doesn’t say it must to be implemented in terms of those, which means<climits>doesn’t have to be included.So the only way you can be guaranteed that a
CHAR_BITis defined is by including<climits>or some other header where it’s explicitly stated it must include it. And as far as I can tell, none have to; an implementation is free to just hard-code the value everywhere it’s needed, for example, or include<limits>and usestd::numeric_limits<unsigned char>::digits(which is equivalent).