I've stumbled across a surprising behavior on mac. When I use smart pointers and compile with apple's clang, it compiles even if I don't include the memory header. This happens when I compile from terminal or use an IDE. When I use clion it doesn't notify me about the missing headers or anything. This causes problems when I try to build on Linux, because the headers are missing. Is there a way where I can force clion or the compiler to be more strict about this?
Asked
Active
Viewed 107 times
1
-
2youre probably including something else which indirectly includes memory. There isn't much you can do, its a common problem with all standard libraries – Alan Birtles Dec 16 '19 at 09:30
-
but why doesn't this work on linux then? – Picard Dec 16 '19 at 09:31
-
3@Picard It is unspecified whether other standard library header files are included when you include one (for the most part). Whether they do differs between compilers and platforms. There is no way of being sure. You need to look at a C++ reference, such as cppreference.com each time you use something from `std::` and look for the header you need to include, otherwise you risk it not working on a different compiler/platform. – walnut Dec 16 '19 at 09:35
-
thanks for comments and answers. I didn't expect iostream to include the smartpointers on mac. – Picard Dec 16 '19 at 09:37
1 Answers
3
The issue here is that the C++ standard only requires that the appropriate #include
brings in the functionality you want into your compilation unit. This is by design and even allows compiler writers to hardcode some C++ standard library functionality into the compiler!
Many C++ standard library implementations #include
headers that bring in C++ standard library functionality implicitly. Some are worse than others in this respect.
There's not much you can really do about this other than being strict about using the mandated #include
s; something you can almost fix by trial and error.

Bathsheba
- 231,907
- 34
- 361
- 483