std::basic_streambuf<CharT,Traits>::setp
From cppreference.com
< cpp | io | basic streambuf
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
Buffers
(C++23)
(C++98/26*)
(C++20)
Streams
Abstractions
File I/O
String I/O
Array I/O
(C++23)
(C++23)
(C++23)
(C++98/26*)
(C++98/26*)
(C++98/26*)
Synchronized Output
(C++20)
Types
Error category interface
(C++11)
(C++11)
std::basic_streambuf
Public member functions
Locales
Positioning
Get area
Put area
Putback
Protected member functions
(C++11)
(C++11)
Locales
Positioning
Get area
Put area
basic_streambuf::setp
Putback
protected:
void setp( char_type* pbeg, char_type* pend );
void setp( char_type* pbeg, char_type* pend );
Sets the values of the pointers defining the put area.
After the call, pbase() == pbeg, pptr() == pbeg and epptr() == pend are all true.
If any of [
pbeg,
pend)
is not a valid range, the behavior is undefined.
Contents
[edit] Parameters
pbeg
-
pointer to the new beginning of the put area
pend
-
pointer to the new end of the put area
[edit] Example
Run this code
#include <array> #include <cstddef> #include <iostream> // Buffer for std::ostream implemented by std::array template<std::size_t size, class CharT = char> struct ArrayedStreamBuffer : std::basic_streambuf <CharT> { using Base = std::basic_streambuf <CharT>; using char_type = typename Base::char_type; ArrayedStreamBuffer() { // put area pointers to work with "buffer" Base::setp(buffer.data(), buffer.data() + size); } void print_buffer() { for (char_type i : buffer) { if (i == 0) std::cout << "\\0"; else std::cout << i; std::cout << ' '; } std::cout << '\n'; } private: std::array <char_type, size> buffer{}; // value-initialize "buffer" }; int main() { ArrayedStreamBuffer<10> streambuf; std::ostream stream(&streambuf); stream << "hello"; stream << ","; streambuf.print_buffer(); }
Output:
h e l l o , 0円 0円 0円 0円
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 4023 | C++98 | setp did not require the output sequence to be a valid range
|
requires |