2

My understanding is that tolower() is a function included in cctype (or in another form in some other libraries, e.g. locale), but I just used it in a program without including anything other than iostream.

Example that compiled for me:

#include <iostream>
int main() {
    char testChar = 'H';
    testChar = tolower(testChar);
    std::cout << testChar << std::endl;
    return 0;
}

Output:

h

How does my program know what tolower() is without including cctype?

KhanKhuu
  • 177
  • 11
  • Are you using an IDE? – M Y Jul 20 '18 at 17:28
  • 1
    Your implementation of `iostream` probably already includes it. You shouldn't rely on that thought. Import what you need, and let the guards sort out the duplicate imports. – Carcigenicate Jul 20 '18 at 17:29
  • 1
    Certain standard headers may include other headers. In your case `` does that. You should not rely on this though. In my eyes, the above is an incorrect program. – DeiDei Jul 20 '18 at 17:29
  • Standard allows standard headers include other standard headers, but doesn't mandate this. So you what you see is an example of unspecified behavior. – SergeyA Jul 20 '18 at 17:30
  • @François Oh you ninja'd me ;). I'll add that dupe from yesterday as well. – πάντα ῥεῖ Jul 20 '18 at 17:33
  • @hellyale yes, I am using an IDE (Xcode). Thanks for the input from everyone else. I figured there was something wrong about it and wanted to check. – KhanKhuu Jul 20 '18 at 17:33
  • 1
    You should profile the build time and your development time for including `cctype` and not including it. My bet is that either the time is insignificant, or you can save development time by redundantly including the file (build time should be insignificant for redundant includes). – Thomas Matthews Jul 20 '18 at 17:36
  • 2
    @ThomasMatthews if all the headers use `#pragma once` as they should, the compiler won't even open the file so the time difference will be zero. – Mark Ransom Jul 20 '18 at 17:39
  • @MarkRansom The files still need to be opened and read for the `#pragma once` or other include guards. Remember that some headers are intrinsic to the compiler so that should reduce the build times. – Thomas Matthews Jul 20 '18 at 20:15
  • 1
    I believe that this issue is a waste of development time. C++ is designed to allow separate module compilation, which reduces the build times; only files that have changed are rebuilt. Linking is fast. It is compilation (parsing and translation) that takes the time. You should only worry about redundant include files when the team is considering overnight builds because of the length of the build process. – Thomas Matthews Jul 20 '18 at 20:19
  • 1
    @ThomasMatthews the thing that makes `#pragma once` special is that it keys off the filename, if a file has already been included once it doesn't need to be opened a second time. That makes it faster than guards. The code I work on every day takes 4 minutes to compile and link from SSD so yes, I worry (a little) about compile times. Waiting for your compiler is a waste. – Mark Ransom Jul 21 '18 at 01:30

0 Answers0