Skip to content

Commit bd4cb84

Browse files
authored
C++17: Add __has_include operator (AnthonyCalandra#112)
1 parent cb50e8e commit bd4cb84

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

CPP17.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ C++17 includes the following new language features:
1818
- [utf-8 character literals](#utf-8-character-literals)
1919
- [direct-list-initialization of enums](#direct-list-initialization-of-enums)
2020
- [fallthrough, nodiscard, maybe_unused attributes](#fallthrough-nodiscard-maybe_unused-attributes)
21+
- [\_\_has\_include](#\_\_has\_include)
2122

2223
C++17 includes the following new library features:
2324
- [std::variant](#stdvariant)
@@ -309,6 +310,43 @@ void my_callback(std::string msg, [[maybe_unused]] bool error) {
309310
}
310311
```
311312
313+
### \_\_has\_include
314+
315+
`__has_include (operand)` operator may be used in `#if` and `#elif` expressions to check whether a header or source file (`operand`) is available for inclusion or not.
316+
317+
One use case of this would be using two libraries that work the same way, using the backup/experimental one if the preferred one is not found on the system.
318+
319+
```c++
320+
#ifdef __has_include
321+
# if __has_include(<optional>)
322+
# include <optional>
323+
# define have_optional 1
324+
# elif __has_include(<experimental/optional>)
325+
# include <experimental/optional>
326+
# define have_optional 1
327+
# define experimental_optional
328+
# else
329+
# define have_optional 0
330+
# endif
331+
#endif
332+
```
333+
334+
It can also be used to include headers existing under different names or locations on various platforms, without knowing which platform the program is running on, OpenGL headers are a good example for this which are located in `OpenGL\` directory on macOS and `GL\` on other platforms.
335+
336+
```c++
337+
#ifdef __has_include
338+
# if __has_include(<OpenGL/gl.h>)
339+
# include <OpenGL/gl.h>
340+
# include <OpenGL/glu.h>
341+
# elif __has_include(<GL/gl.h>)
342+
# include <GL/gl.h>
343+
# include <GL/glu.h>
344+
# else
345+
# error No suitable OpenGL headers found.
346+
# endif
347+
#endif
348+
```
349+
312350
## C++17 Library Features
313351

314352
### std::variant

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ C++17 includes the following new language features:
4949
- [utf-8 character literals](#utf-8-character-literals)
5050
- [direct-list-initialization of enums](#direct-list-initialization-of-enums)
5151
- [fallthrough, nodiscard, maybe_unused attributes](#fallthrough-nodiscard-maybe_unused-attributes)
52+
- [\_\_has\_include](#\_\_has\_include)
5253

5354
C++17 includes the following new library features:
5455
- [std::variant](#stdvariant)
@@ -960,6 +961,43 @@ void my_callback(std::string msg, [[maybe_unused]] bool error) {
960961
}
961962
```
962963
964+
### \_\_has\_include
965+
966+
`__has_include (operand)` operator may be used in `#if` and `#elif` expressions to check whether a header or source file (`operand`) is available for inclusion or not.
967+
968+
One use case of this would be using two libraries that work the same way, using the backup/experimental one if the preferred one is not found on the system.
969+
970+
```c++
971+
#ifdef __has_include
972+
# if __has_include(<optional>)
973+
# include <optional>
974+
# define have_optional 1
975+
# elif __has_include(<experimental/optional>)
976+
# include <experimental/optional>
977+
# define have_optional 1
978+
# define experimental_optional
979+
# else
980+
# define have_optional 0
981+
# endif
982+
#endif
983+
```
984+
985+
It can also be used to include headers existing under different names or locations on various platforms, without knowing which platform the program is running on, OpenGL headers are a good example for this which are located in `OpenGL\` directory on macOS and `GL\` on other platforms.
986+
987+
```c++
988+
#ifdef __has_include
989+
# if __has_include(<OpenGL/gl.h>)
990+
# include <OpenGL/gl.h>
991+
# include <OpenGL/glu.h>
992+
# elif __has_include(<GL/gl.h>)
993+
# include <GL/gl.h>
994+
# include <GL/glu.h>
995+
# else
996+
# error No suitable OpenGL headers found.
997+
# endif
998+
#endif
999+
```
1000+
9631001
## C++17 Library Features
9641002

9651003
### std::variant

0 commit comments

Comments
 (0)