for co_await (for-range-declaration: expression) statement
用处
简化如下问题的实现:
generator
异步I/O
延迟计算
事件驱动的程序
例子(VC++)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
experimental::generator<int> GetSequenceGenerator( int startValue, size_t numberOfValues) { for (int i = 0 startValue; i < startValue + numberOfValues; ++i){ time_t t = system_clock::to_time_t(system_clock::now()); cout << std:: ctime(&t); co_yield i; } } int main() { auto gen = GetSequenceGenerator(10, 5); for (const auto& value : gen) { cout << value << "(Press enter for next value)" << endl; cin.ignore(); } }
class Point { int x; int y; public: friend bool operator==(const Point& a, const Point& b){ return a.x==b.x && a.y==b.y; } friend bool operator< (const Point& a, const Point& b){ return a.x < b.x || (a.x == b.x && a.y < b.y); } friend bool operator!=(const Point& a, const Point& b) { return !(a==b); } friend bool operator<=(const Point& a, const Point& b) { return !(b<a); } friend bool operator> (const Point& a, const Point& b) { return b<a; } friend bool operator>=(const Point& a, const Point& b) { return !(a<b); } // ... 其他非比较函数 ... }; #include <compare> class Point { int x; int y; public: auto operator<=>(const Point&) const = default; // 比较操作符自动生成 // ... 其他非比较函数 ... };
标准库类型支持 <=>
vector, string, map, set, sub_match, …
例如:
范围 for 循环语句支持初始化语句
switch 语句初始化 (C++17):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct Foo { int value; int result; }; Foo GetData() { return Foo(); }
int main() { switch (auto data = GetData(); data.value) { case 1: return data.result; } }
if 语句初始化 (C++17):
1 2 3 4 5 6 7 8 9 10 11 12 13
struct Foo { int value; int result; }; Foo* GetData() { return new Foo(); }
int main() { if (auto data = GetData(); data) { // Use 'data’ } }
现在范围 for 循环同样支持初始化 (C++20):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct Foo { std::vector<int> values; }; Foo GetData() { return Foo(); } int main() { for (auto data = GetData(); auto& value : data.values) { // Use 'data’ } }
非类型模板形参支持字符串
1 2 3 4 5 6 7
template<auto& s> void DoSomething() { std::cout << s << std::endl; } int main() { DoSomething<"CppCon">(); }
[[likely]], [[unlikely]]
先验概率指导编译器优化
1 2 3 4 5 6
switch (value) { case 1: break; [[likely]] case 2: break; [[unlikely]] case 3: break; }
日历(Calendar)和时区(Timezone)功能
增加日历和时区的支持
只支持公历(Gregorian calendar)
其他日历也可通过扩展加入, 并能和 进行交互
初始化 年, 月 日的方法
1 2 3 4 5 6 7 8 9 10
// creating a year auto y1 = year{ 2019 }; auto y2 = 2019y; // creating a mouth auto m1 = month{ 9 }; auto m2 = September; // creating a day auto d1 = day{ 18 }; auto d2 = 18d;
创建完整的日期
1 2 3 4
year_mouth_day fulldate1{2019y, September, 18d}; auto fulldate2 = 2019y / September / 18d; year_mouth_day fulldate3{Monday[3]/September/2019}; // Monday[3] 表示第三个星期一
新的事件间隔单位, 类似于秒, 分钟, …
1 2 3 4 5
using days = duration<signed interger type of at least 25bits, ratio_multiply<ratio<24>, hours::period>>; using weeks = ...; using mouths = ...; using years = ...;
utc_clock: represents Coordinated Universal Time (UTC), measures time since 00:00:00 UTC, Thursday, 1 January 1970, including leap seconds
tai_clock: represents International Atomic Time (TAI), measures time since 00:00:00, 1 January 1958, and was offseted 10 seconds ahead of UTC at that date, it does not include leap seconds
gps_clock: represents Global Positioning System (GPS) time, measures time since 00:00:00, 6 January 1980 UTC, it does not include leap seconds
file_clock: alias for the clock used for std::filesystem::file_time_type, epoch is unspecified
新增system_clock相关的别名
1 2 3 4 5 6 7 8
template<class Duration> using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>; using sys_seconds = sys_time<std::chrono::seconds>; using sys_days = sys_time<std::chrono::days>; // 用例: system_clock::time_point t = sys_days{ 2019y / September / 18d }; // date -> time_point auto yearmonthday = year_month_day{ floor<days>(t) }; // time_point -> date
日期 + 事件
1 2
auto t = sys_days{2019y/September/18d} + 9h + 35min + 10s; // 2019-09-18 09:35:10 UTC
时区转换
1 2 3 4 5 6 7 8 9
// Convert UTC to Denver time: zoned_time denver = { "America/Denver", t }; // Construct a local time in Denver: auto t = zoned_time{ "America/Denver", local_days{Wednesday[3] / September / 2019} + 9h }; // Get current local time: auto t = zoned_time{ current_zone(), system_clock::now() };
std::span
头文件
某段连续数据的”视图”
不持有数据, 不分配和销毁数据
拷贝非常快, 推荐复制的方式传参(类似 string_view)
不支持数据跨步(stride)
可通过运行期确定长度也可编译器确定长度
1 2 3 4 5
int data[42]; span<int, 42> a {data}; // fixed-size: 42 ints span<int> b {data}; // dynamic-size: 42 ints span<int, 50> c {data}; // compilation error span<int> d{ ptr, len }; // dynamic-size: len ints