名前空間
変種
操作

std::basic_ospanstream<CharT,Traits>::span

From cppreference.com
 
 
 
std::basic_ospanstream
メンバ関数
基になるバッファ操作
basic_ospanstream::span
(C++23)
非メンバ関数
 
std::span<CharT> span() const noexcept;
(1) (C++23から)
void span( std::span<CharT> s ) noexcept;
(2) (C++23から)
1) ラップされた std::basic_spanbuf のオープンモードに std::ios_base::out が設定されている場合、書き込み領域を参照するspanを返します。それ以外の場合は、基底バッファを参照するspanを返します。
2) ラップされた std::basic_spanbuf に、s が参照するバッファに対する I/O を行わせます。

目次

[編集] パラメータ

s - ストリームの新しい基底バッファとして使用されるストレージを参照するstd::span

[編集] 戻り値

1) ラップされた std::basic_spanbuf のオープンモードに応じて、基底バッファまたは書き込み領域を参照する std::span
2) (なし)

[編集]

#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> の public メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)