__has_include
Use case
Portable header detection.
Explanation
__has_include checks if a header exists at preprocessing time.
Code
#include <iostream>
#if __has_include(<mach-o/loader.h>)
#define BINARY_FORMAT "Mach-O (macOS/iOS)"
#elif __has_include(<elf.h>)
#define BINARY_FORMAT "ELF (Linux)"
#else
#define BINARY_FORMAT "unknown"
#endif
int main() {
std::cout << "Binary format: " << BINARY_FORMAT << "\n";
return 0;
}
Output
$ ./src/c++17/build/has-include
Binary format: Mach-O (macOS/iOS)