I noticed that clang++ includes a missing header - <limits>
on Mac, while g++ shows errors about it on Linux. Now I wonder why clang does it, and gcc not. And how I can force clang to not do it.
Here is a sample code which compiles by clang on Mac, but not by gcc on Linux:
#include <iostream>
using namespace std;
int main()
{
cout << "int max: " << numeric_limits<int>::max() << endl;
}
UPD
I looked into libraries and here is what I found.
Internally <iostream>
includes <istream>
, which defines >>
operator for different types. <istream>
wants to know limits for short
, int
and streamsize
types.
clang++ uses libc++ standard library, which uses std::numeric_limits
class template from <limits>
in <istream>
for this purpose. That's why this header is included automatically when <iostream>
is included.
g++ uses libstdc++ standard library, which uses __gnu_cxx::__numeric_traits
class template from <ext/numeric_traits.h>
instead of using <limits>
in <istream>
(<bits/istream.tcc>
). There is also a comment in that header which explains why they don't use <limits>
:
<limits>
is big and we avoid including it
Used compilers:
> clang++ --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
$ g++ --version
g++ (Debian 4.9.2-10) 4.9.2