Skip to content

Commit 2bfc958

Browse files
committed
book: add C++17 __has_include preprocessor operator
Add a 2.7 subsection on __has_include for portable conditional header inclusion, with a compiled example (code/2/2.27). Refs #1
1 parent 78bed5e commit 2bfc958

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

book/en-us/02-usability.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,21 @@ static_assert(!is_signed_integral<unsigned>);
11791179
static_assert(std::negation_v<std::is_floating_point<int>>);
11801180
```
11811181

1182+
### `__has_include`
1183+
1184+
C++17 standardized the preprocessor operator `__has_include`, which checks at compile time whether a header is available, enabling portable conditional inclusion:
1185+
1186+
```cpp
1187+
#if __has_include(<optional>)
1188+
# include <optional>
1189+
# define HAS_OPTIONAL 1
1190+
#else
1191+
# define HAS_OPTIONAL 0
1192+
#endif
1193+
```
1194+
1195+
This is very useful when supporting different standard-library versions, or providing a fallback when a feature might be missing.
1196+
11821197
## Conclusion
11831198

11841199
This section introduces the enhancements to language usability in modern C++, which I believe are the most important features that almost everyone needs to know and use:

book/zh-cn/02-usability.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,21 @@ static_assert(!is_signed_integral<unsigned>);
10931093
static_assert(std::negation_v<std::is_floating_point<int>>);
10941094
```
10951095

1096+
### `__has_include`
1097+
1098+
C++17 标准化了预处理器运算符 `__has_include`,用于在编译期检查某个头文件是否可用,从而写出可移植的条件包含逻辑:
1099+
1100+
```cpp
1101+
#if __has_include(<optional>)
1102+
# include <optional>
1103+
# define HAS_OPTIONAL 1
1104+
#else
1105+
# define HAS_OPTIONAL 0
1106+
#endif
1107+
```
1108+
1109+
这在需要兼容不同标准库版本,或在某个特性可能缺失时提供回退实现的场景中非常有用。
1110+
10961111
## 总结
10971112

10981113
本节介绍了现代 C++ 中对语言可用性的增强,其中笔者认为最为重要的几个特性是几乎所有人都需要了解并熟练使用的:

code/2/2.27.has.include.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// 2.27.has.include.cpp
3+
// chapter 2 language usability
4+
// modern cpp tutorial
5+
//
6+
// created by changkun at changkun.de
7+
// https://github.com/changkun/modern-cpp-tutorial
8+
//
9+
10+
#include <iostream>
11+
12+
// C++17: __has_include is a preprocessor operator that checks whether a
13+
// header is available, enabling portable conditional inclusion.
14+
#if __has_include(<optional>)
15+
# include <optional>
16+
# define HAS_OPTIONAL 1
17+
#else
18+
# define HAS_OPTIONAL 0
19+
#endif
20+
21+
int main() {
22+
std::cout << "<optional> available: " << HAS_OPTIONAL << std::endl;
23+
#if HAS_OPTIONAL
24+
std::optional<int> o = 42;
25+
std::cout << "value: " << o.value() << std::endl;
26+
#endif
27+
}

0 commit comments

Comments
 (0)