C++17引入了许多实用的新特性,下面为你详细介绍并举例说明:
### 1. 结构化绑定
结构化绑定能把结构体、元组或者数组的成员直接绑定到变量上,使代码更简洁直观。
“`cpp
#include <iostream>
#include <utility>
int main() {
std::pair<int, double> p = {1, 2.3};
auto [a, b] = p;
std::cout << “a: ” << a << “, b: ” << b << std::endl;
return0;
}
“`
在这个例子中,通过 `auto [a, b] = p;` 把 `std::pair` 的两个成员分别绑定到变量 `a` 和 `b` 上。
### 2. `if constexpr`
`if constexpr` 用于在编译时进行条件判断,避免运行时开销。这在模板编程里很有用。
“`cpp
#include <iostream>
#include <type_traits>
template <typename T>
auto get_value(T t) {
ifconstexpr (std::is_integral_v<T>) {
return t * 2;
} else {
return t;
}
}
int main() {
std::cout << get_value(5) << std::endl;
std::cout << get_value(3.14) << std::endl;
return0;
}
“`
此例中,`if constexpr` 会在编译时依据 `T` 是否为整数类型来选择执行哪个分支。
### 3. `std::filesystem`
`std::filesystem` 提供了跨平台的文件系统操作功能,让文件和目录的操作更方便。
“`cpp
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
fs::path p = “test.txt”;
if (fs::exists(p)) {
std::cout << “File exists.” << std::endl;
} else {
std::cout << “File does not exist.” << std::endl;
}
return0;
}
“`
这个例子检查 `test.txt` 文件是否存在。
### 4. 内联变量
C++17允许在类或者命名空间中定义内联变量,避免了重复定义的问题。
“`cpp
#include <iostream>
struct MyClass {
inlinestaticint value = 10;
};
int main() {
std::cout << MyClass::value << std::endl;
return0;
}
“`
这里 `value` 是 `MyClass` 的内联静态变量。
### 5. 折叠表达式
折叠表达式简化了可变参数模板的使用,让对可变参数包的操作更简洁。
“`cpp
#include <iostream>
template<typename… Args>
auto sum(Args… args) {
return (args + …);
}
int main() {
std::cout << sum(1, 2, 3, 4) << std::endl;
return0;
}
“`
此例中,`(args + …)` 是一个折叠表达式,用于计算所有参数的和。
### 6. `std::optional`
`std::optional` 表示一个可能存在的值,可避免使用指针或者特殊值来表示缺失值。
“`cpp
#include <iostream>
#include <optional>
std::optional<int> get_value(bool condition) {
if (condition) {
return42;
}
returnstd::nullopt;
}
int main() {
auto result = get_value(true);
if (result) {
std::cout << “Value: ” << *result << std::endl;
} else {
std::cout << “No value.” << std::endl;
}
return0;
}
“`
在这个例子中,`get_value` 函数返回一个 `std::optional<int>`,依据条件决定是否有值。
code : #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <ranges> #include <vector> #include <filesystem> template <typename T> auto getValue(T t) { if constexpr (std::is_integral_v<T>) { return t * 2; } return t; } namespace fs = std::filesystem; namespace { inline int val = 10; } template <typename... Args> void print(Args... args) { ((std::cout << args << " "), ...); } template<typename... Args> auto sum(Args... args) { return (args + ...); } std::optional<int> get_value(bool condition) { if (condition) { return 42; } return std::nullopt; } int main() { // c++ 17 // new feature 1 // Structured Binding // core : depend on you state sturcture kind to create corresponding value when int compiling stage. std::tuple tuple = {1, 3, 5.0, "string"}; auto [a, b, c, d] = tuple; std::cout << a << " " << b << " " << c << " " << d << std::endl; struct StructuredBind { int a; double b; char* c; }; StructuredBind structuredBind = {1, 5.0, "string"}; auto [aa, bb, cc] = structuredBind; //new feature 2 // constexpr : judge when compile stage! std::cout << getValue(5) << std::endl; std::cout << getValue(3.14) << std::endl; // new feature 3 // file system fs::path p = fs::current_path(); std::cout << p.string() << std::endl; std::cout << fs::is_directory(p) << std::endl; if (fs::exists(p)) { std::cout << "file exist now!" << fs::is_directory(p) << std::endl; } else { std::cout << "file does not exist :" << fs::is_directory(p) << std::endl; } // new feature 4 // inline variable /* * use inline state global value (in namespace ) or static value (int class) , avoid redefine! */ // new feature 5 // ... means that all arguments will rank like first two element one element std::cout << sum(1, 2, 3, 4, 5) << std::endl; // two element print(1, 2, 3, 4, 5); // core understanding // handle multiple number and multiple category value with different guid! /*symbol ... , *. one element by sequence use option, (pack op ...) *. two element start with first param then count together! (pack op ... op init) */ // new feature 6 auto result = get_value(true); if (result) { std::cout << *result << std::endl; } else { std::cout << "no value"<< std::endl; } return 0; }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)