可以的,CentOS 是支持 C++17 特性的,但需要注意系统版本和编译器版本。
下面详细说明:
gcc --version
g++ -std=c++17 test.cpp -o test
yum install centos-release-scl
yum install devtoolset-11-gcc devtoolset-11-gcc-c++
scl enable devtoolset-11 bash
g++ -std=c++17 test.cpp
devtoolset-7 及以上均支持 C++17
yum install clang
clang++ -std=c++17 test.cpp
✅ 完全支持(GCC ≥ 7):
std::optionalstd::variantstd::string_viewif constexprstd::filesystem⚠ 注意:
std::filesystem 在 GCC 8 才稳定-lstdc++fs#include <iostream>
#include <variant>
int main() {
std::variant<int, std::string> v = "hello";
std::visit([](auto&& x){
std::cout << x << std::endl;
}, v);
}
编译:
g++ -std=c++17 test.cpp
✅ CentOS 支持 C++17
❗ CentOS 7 必须升级 GCC / Clang
✅ CentOS 8 及以上可直接使用
如果你愿意,我可以: