我想使用 C++23 的
<expected>
,但 clang 无法在我的系统上找到它,除非我添加 -stdlib=libc++
,这会导致针对 libstdc++
编译的各种共享库出现其他错误。
我安装了 gcc-12,
<expected>
存在于 /usr/include/c++/12
。
clang 似乎正在使用 gcc-12 作为候选者:
$ clang++-17 -v
Ubuntu clang version 17.0.6 (++20231208085857+6009708b4367-1~exp1~20231208085958.76)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
如果使用
gcc-12
作为候选,为什么 /usr/include/c++/12
中的标题没有被拾取?
你确定不是找到头,而是找到头但不编译内容?
您是否遇到类似以下错误:
test.cc:1:10: fatal error: 'expected' file not found
或者您是否收到类似以下错误:
test.cc:10:6: error: no template named 'expected' in namespace 'std'
?
std::expected
的 GCC 12 实现基于以下功能测试宏:
#if __cplusplus > 202002L && __cpp_concepts >= 202002L
Clang 17 与
-std=c++2b
定义:
#define __cplusplus 202101L
#define __cpp_concepts 201907L
这意味着当它编译
<expected>
标头时,它只会将其视为完全空的标头,而不定义 std::expected
。
GCC 需要
__cpp_concepts >= 202002L
的原因是因为它依赖于 P0848R3 “Conditionally Trivial Special Member Functions”,而 Clang 显然不支持。