{"id":1762,"date":"2025-04-13T13:15:17","date_gmt":"2025-04-13T20:15:17","guid":{"rendered":"https:\/\/nanzhou.cc\/?p=1762"},"modified":"2025-04-13T13:15:28","modified_gmt":"2025-04-13T20:15:28","slug":"c-20-features","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2025\/04\/13\/c-20-features\/","title":{"rendered":"C++ 20 Features"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Coroutines<\/h2>\n\n\n\n<p><em>Coroutines<\/em>\u00a0are special functions that can have their execution suspended and resumed. To define a coroutine, the\u00a0<code>co_return<\/code>,\u00a0<code>co_await<\/code>, or\u00a0<code>co_yield<\/code>\u00a0keywords must be present in the function&#8217;s body. C++20&#8217;s coroutines are stackless; unless optimized out by the compiler, their state is allocated on the heap.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">task&lt;> tcp_echo_server()\n{\n    char data[1024];\n    while (true)\n    {\n        std::size_t n = co_await socket.async_read_some(buffer(data));\n        co_await async_write(socket, buffer(data, n));\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Designated initializers<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">struct A {\n  int x;\n  int y;\n  int z = 123;\n};\n\nA a {.x = 1, .z = 2}; \/\/ a.x == 1, a.y == 0, a.z == 2<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/github.com\/AnthonyCalandra\/modern-cpp-features\/blob\/master\/CPP20.md#designated-initializers\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Formatting library<\/h2>\n\n\n\n<p>Combine the simplicity of\u00a0<code>printf<\/code>\u00a0with the type-safety of\u00a0<code>iostream<\/code>. Uses braces as placeholders, and supports custom formatting similar to printf-style specifiers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">std::format(\"{1} {0}\", \"world\", \"hello\"); \/\/ == \"hello world\"\n\nint x = 123;\nstd::string str = std::format(\"x: {}\", x); \/\/ str == \"x: 123\"\n\n\/\/ Format to an output iterator:\nfor (auto x : {1, 2, 3}) {\n  std::format_to(std::ostream_iterator&lt;char>{std::cout, \"\\n\"}, \"{}\", x);\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/github.com\/AnthonyCalandra\/modern-cpp-features\/blob\/master\/CPP20.md#formatting-library\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">std::span<\/h2>\n\n\n\n<p>A span is a view (i.e. non-owning) of a container providing bounds-checked access to a contiguous group of elements. Since views do not own their elements they are cheap to construct and copy &#8212; a simplified way to think about views is they are holding references to their data. As opposed to maintaining a pointer\/iterator and length field, a span wraps both of those up in a single object.<\/p>\n\n\n\n<p>Span doesn&#8217;t propogate const so to construct a read-only span use\u00a0<code>std::span&lt;const T><\/code>.<a href=\"https:\/\/github.com\/AnthonyCalandra\/modern-cpp-features\/blob\/master\/CPP20.md#stdspan\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">void print_ints(std::span&lt;const int> ints) {\n    for (const auto n : ints) {\n        std::cout &lt;&lt; n &lt;&lt; std::endl;\n    }\n}\n\nprint_ints(std::vector{ 1, 2, 3 });\nprint_ints(std::array&lt;int, 5>{ 1, 2, 3, 4, 5 });\n\nint a[10] = { 0 };\nprint_ints(a);\n\/\/ etc.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Coroutines Coroutines\u00a0are special functions that can have their execution suspended and resumed. To define a coroutine, the\u00a0co_return,\u00a0co_await, or\u00a0co_yield\u00a0keywords must be present in the function&#8217;s body. C++20&#8217;s coroutines are stackless; unless optimized out by the compiler, their state is allocated on the heap. Designated initializers Formatting library Combine the simplicity of\u00a0printf\u00a0with the type-safety of\u00a0iostream. Uses braces&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"0","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-1762","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=1762"}],"version-history":[{"count":1,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1762\/revisions"}],"predecessor-version":[{"id":1763,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1762\/revisions\/1763"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=1762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=1762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=1762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}