C++20 STL Features: 1 Year of Development on GitHub} cout << i << endl; // ordinary read, 1729 }21 span22 span Encapsulates Ptr-Len Pairs • span is a non-owning view of contiguous elements • spanis like a pair of T* and size_t, but better • spans accidental pointer arithmetic • span span ; span 🐞 span • Shallow const, like T* • span can be reassigned, can't modify elements • const span can't be reassigned, • MSVC's std::span checks debug; gsl::span always23 Contiguous Range Constructor • GH-142, GH-500, GH-587 implemented by miscco #include #include #include <span> #include 0 码力 | 45 页 | 702.09 KB | 6 月前3
C++20: An (Almost) Complete OverviewInitializer Non-Type Template Parameters [[likely]] and [[unlikely]] Calendars & Timezones std::span Feature Test Macros Immediate Functions – consteval constinit Class Enums system_clock::now() } }; Output: cout << localt << endl; // 2020-09-15 09:35:10.15365 std::span <span> Provides a “view” over some contiguous data Does not own the data Never allocates/deallocates support strides66 std::span Can be dynamic-sized (run time) or fixed-sized (compile time) Examples: int data[42]; span a {data}; // fixed-size: 42 ints span b {data}; // 0 码力 | 85 页 | 512.18 KB | 6 月前3
Bringing Existing Code to CUDA Using constexpr and std::pmri < n; i++) y[i] = x[i] + y[i]; } __global__ void add_gpu( int n, gsl::spanx, gsl::span y) { for (int i = 0; i < n; i++) y[i] = x[i] + y[i]; } 22 |Avoid auto operator()(index i) const { return a_[i] + b_[i]; } }; struct add_span { gsl::span a_; gsl::span b_; auto operator()(index i) const { return a_[i] + + b_[i]; } }; template void add(int n, callable f, gsl::span y) { for (int i = 0; i < n; i++) y[i] += f(i); } 23 |“Unified Memory creates a pool of managed memory that is shared 0 码力 | 51 页 | 3.68 MB | 6 月前3
C++23: An Overview of Almost All New and Updated FeaturesDefined in std::mdspan Multidimensional array view Multidimensional extension of std::span (from C++20) Supports different layout policies std::submdspan An mdspan viewing a subset data[] = "11 22"; std::ispanstream s { std::span {data} }; int a, b; s >> a >> b; E.g. output: char data[32] {}; std::ospanstream s { std::span {data} }; s << 22 << 11;96 Agenda C++23 0 码力 | 105 页 | 759.96 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - 15 C++ 系列课:字符与字符串char *ptr; • size_t len; }; • void cihou_array(FatPtr fptr); • 这就是 rust 炫耀已久的数组胖指针。 C++20 中的 span 也是这个思想。 • 他提倡把 ptr 和 len 这两个逻辑上相关的参数绑在一起,避免程序员犯错。 用胖指针表示字符串 • 其实胖指针的思想既不是 rust 原创的,也不是 C++20 以后才出现的。 c_str 时)。 • 长度已经存储在 string 的成员里, size() 是 O(1) 的。 • 在尾部切片可以用 resize() 修改长度,无需写入字符串本身。 • string_view 和 span 无非是个弱引用版本,额外增加了在头部切片的能力而 已。 强引用胖指针: string • 刚刚说的 string 容器,是掌握着字符串生命周期( lifespan )的胖指针。 • 这种掌 string_view 以后,不要改写原字符串。 举例:常见容器及其相应的弱引用 强引用 弱引用 string string_view wstring wstring_view vectorspan unique_ptr T * shared_ptr weak_ptr 字符串用 substr 切片 • 熟悉 Python 的同学对切片 (slice) 操作肯定不陌生,例如: 0 码力 | 162 页 | 40.20 MB | 1 年前3
Working with Asynchrony Generically: A Tour of C++ Executors};42 SENDERS AND COROUTINES43 // This is a coroutine: unifex::taskread_socket_async(socket, span ); int main() { socket s = /*...*/; char buff[1024]; auto [cbytes] = std::this_thread::sync_wait( a coroutine type trivially. // This is a coroutine: unifex::task read_socket_async(socket, span ); unifex::task concurrent_read_async(socket s1, socket s2) { char buff1[1024]; 0 码力 | 121 页 | 7.73 MB | 6 月前3
共 6 条
- 1













