std::strstr...strstreambuf(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::strstreambuf::strstreambuf πŸ’–

okay bestie, welcome to the std::strstreambuf::strstreambuf page from the C++ Standard Libary manual β€” page name std::strstr...strstreambuf(3). yes the title is a lil cursed, but we move. πŸ’…


🌸 NAME 🌸

std::strstreambuf::strstreambuf β€” std::strstreambuf::strstreambuf


✨ Synopsis ✨

okay so the source page's columns are giving chaotic group-chat energy, but here are the overloads with their numbers and annotations kept intact, bestie:

  • (1) explicit strstreambuf( std::streamsize alsize = 0 ); β€” (deprecated in C++98), (until C++11)
  • (1) strstreambuf() : strstreambuf(0) {} β€” (since C++11), (removed in C++26)
  • (1) explicit strstreambuf( std::streamsize alsize ); β€” (deprecated in C++98), (removed in C++26)
  • (2) strstreambuf( void* (*palloc)(std::size_t), void (*pfree)(void*) ); β€” (deprecated in C++98), (removed in C++26)
  • (3) strstreambuf( char* gnext, std::streamsize n, char* pbeg = 0 ); β€” (deprecated in C++98), (removed in C++26)
  • (4) strstreambuf( signed char* gnext, std::streamsize n, signed char* pbeg = 0 ); β€” (deprecated in C++98), (removed in C++26)
  • (5) strstreambuf( unsigned char* gnext, std::streamsize n, unsigned char* pbeg = 0 ); β€” (deprecated in C++98), (removed in C++26)
  • (6) strstreambuf( const char* gnext, std::streamsize n ); β€” (deprecated in C++98), (removed in C++26)
  • (7) strstreambuf( const signed char* gnext, std::streamsize n ); β€” (deprecated in C++98), (removed in C++26)
  • (8) strstreambuf( const unsigned char* gnext, std::streamsize n ); β€” (deprecated in C++98), (removed in C++26)

🧚 what each overload is actually doing 🧚

  • 1) constructs a std::strstreambuf object: initializes the base class by calling the default constructor of std::streambuf, initializes the buffer state to "dynamic" (the buffer will be allocated as needed), initializes allocated size to the provided alsize, initializes the allocation and the deallocation functions to null (will use new[] and delete[]).
  • 2) constructs a std::strstreambuf object: initializes the base class by calling the default constructor of std::streambuf, initializes the buffer state to "dynamic" (the buffer will be allocated as needed), initializes allocated size to an unspecified value, initializes the allocation function to palloc and the deallocation function to pfree.
  • 3-5) constructs a std::strstreambuf object in the following steps:
    • a) initializes the base class by calling the default constructor of std::streambuf.
    • b) initializes the buffer state to "constant" (the buffer is a user-provided fixed-size buffer).
    • c) determines the number of elements in the user-provided array as follows: if n is greater than zero, n is used. If n is zero, std::strlen(gnext) is executed to determine the buffer size. If n is negative, INT_MAX is used.
    • d) configures the std::basic_streambuf pointers as follows: if pbeg is a null pointer, calls setg(gnext, gnext, gnext + N). If pbeg is not a null pointer, executes setg(gnext, gnext, pbeg) and setp(pbeg, pbeg + N), where N is the number of elements in the array as determined earlier.
  • 6-8) same as strstreambuf((char*)gnext, n), except the "constant" bit is set in the buffer state bitmask β€” output to this buffer is not allowed. 🚨

πŸŽ€ Parameters πŸŽ€

  • alsize β€” the initial size of the dynamically allocated buffer
  • palloc β€” pointer to user-provided allocation function
  • pfree β€” pointer to user-provided deallocation function
  • gnext β€” pointer to the start of the get area in the user-provided array
  • pbeg β€” pointer to the start of the put area in the user-provided array
  • n β€” the number of bytes in the get area (if pbeg is null) or in the put area (if pbeg is not null) of the user-provided array

πŸ¦‹ Notes πŸ¦‹

these constructors are typically called by the constructors of std::strstream.

🩹 Defect reports 🩹

the following behavior-changing defect reports were applied retroactively to previously published C++ standards:

  • P0935R0 β€” applied to C++11. Behavior as published: default constructor was explicit. Correct behavior: made implicit.

🌈 Example 🌈

// Run this code

#include <iostream>
#include <strstream>

int main()
{
    std::strstreambuf dyn; // dynamic
    std::strstream dyn_s; // equivalent stream
    dyn_s << 1.23 << std::ends;
    std::cout << dyn_s.str() << '\n';
    dyn_s.freeze(false);

    char buf[10];
    std::strstreambuf user(buf, 10, buf); // user-provided output buffer
    std::ostrstream user_s(buf, 10); // equivalent stream
    user_s << 1.23 << std::ends;
    std::cout << buf << '\n';

    std::strstreambuf lit("1 2 3", 5); // constant
    std::istrstream lit_s("1 2 3"); // equivalent stream
    int i, j, k;
    lit_s >> i >> j >> k;
    std::cout << i << ' ' << j << ' ' << k << '\n';
}

πŸ“€ Output πŸ“€

1.23
1.23
1 2 3

πŸ”₯ See also πŸ”₯

  • constructor β€” constructs a strstream object, optionally allocating the buffer (public member function of std::strstream)

mwah πŸ’– this page came from http://cppreference.com, dated 2024.06.10, and it's the std::strstr...strstreambuf(3) page. byeee 🫢

πŸ“¦ where this page comes from