std::basic_ispanstream<CharT,Traits>::span
來自 cppreference.com
< cpp | io | basic ispanstream
std::span<const CharT> span() const noexcept; |
(1) | (C++23 起) |
void span( std::span<CharT> s ) noexcept; |
(2) | (C++23 起) |
template< class SpanLike > void span( SpanLike&& r ) noexcept; |
(3) | (C++23 起) |
2) 使包裝的 std::basic_spanbuf 在 s 引用的緩衝區上執行 I/O 操作。
3) 與 (2) 相同,除了 s 的獲取方式如下:
std::span<const CharT> cs{std::forward<SpanLike>(r)};
std::span<CharT> s{const_cast<CharT*>(cs.data()), cs.size()};
。此過載僅在
std::span<const CharT> cs{std::forward<SpanLike>(r)};
std::span<CharT> s{const_cast<CharT*>(cs.data()), cs.size()};
。此過載僅在
SpanLike
滿足 borrowed_range
,std::convertible_to<SpanLike, std::span<CharT>> 為 false,並且 std::convertible_to<SpanLike, std::span<const CharT>> 為 true 時參與過載決議。目錄 |
[編輯] 引數
s | - | std::span 引用用作流的新底層緩衝區的儲存 |
r | - | borrowed_range 用作流的新底層緩衝區 |
[編輯] 返回值
1) 一個 std::span,根據包裝的 std::basic_spanbuf 的開啟模式,引用底層緩衝區或寫入區域。
2,3) (無)
[編輯] 示例
執行此程式碼
#include <cassert> #include <iostream> #include <span> #include <spanstream> int main() { char out_buf[16]; std::ospanstream ost{std::span<char>{out_buf}}; ost << "C++" << ' ' << 23 << '\0'; // note explicit null-termination auto sp = ost.span(); assert( sp[0] == 'C' && sp[1] == '+' && sp[2] == '+' && sp[3] == ' ' && sp[4] == '2' && sp[5] == '3' && sp[6] == '\0' ); std::cout << "sp.data(): [" << sp.data() << "]\n"; std::cout << "out_buf: [" << out_buf << "]\n"; // spanstream uses out_buf as internal storage, no allocations assert(static_cast<char*>(out_buf) == sp.data()); const char in_buf[] = "X Y 42"; std::ispanstream ist{std::span<const char>{in_buf}}; assert(static_cast<const char*>(in_buf) == ist.span().data()); char c; ist >> c; assert(c == 'X'); ist >> c; assert(c == 'Y'); int i; ist >> i; assert(i == 42); ist >> i; // buffer is exhausted assert(!ist); }
輸出
sp.data(): [C++ 23] out_buf: [C++ 23]
[編輯] 參閱
根據模式獲取或初始化底層緩衝區 ( std::basic_spanbuf<CharT,Traits> 的公共成員函式) |