std::strstreambuf::setbuf(3)

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

std::strstreambuf::setbuf(3) πŸ’…

omg hi bestie πŸ’– welcome to the C++ Standard Libary page for std::strstreambuf::setbuf(3) β€” yes, the manual name is spelled Libary up there, and we're keeping that historical typo exactly as it appeared, no notes. 🫢 This one is giving protected-virtual legacy energy, so buckle up.


πŸ’– NAME πŸ’–

std::strstreambuf::setbuf β€” std::strstreambuf::setbuf (yes, the name repeats itself; it's iconic, leave it πŸ’…)


✨ Synopsis ✨

protected:                                                (deprecated
in C++98)
virtual streambuf* setbuf( char* s, std::streamsize n );  (removed in
C++26)

Okay so this function is deprecated in C++98 and removed in C++26, which is a whole timeline, ngl. πŸ’€

If s is a null pointer and n is zero, this function has no effect.

Otherwise, the effect is implementation-defined: some implementations do nothing, while some implementations deallocate the dynamic member array used as the buffer and begin using the user-supplied character array of size n, whose first element is pointed to by s.

This function is protected virtual, it may only be called through pubsetbuf() or from member functions of a user-defined class derived from std::strstreambuf.


πŸŽ€ Parameters πŸŽ€

  • s β€” pointer to the first byte in the user-provided buffer.
  • n β€” the number of bytes in the user-provided buffer.

πŸ’– Return value πŸ’–

this β€” that's the whole answer, bestie. 🫢


🌸 Example 🌸

Implementation test to check if setbuf() is supported on a dynamic strstream (output obtained with Sun Studio):

// Run this code

#include <iostream>
#include <strstream>

int main()
{
    char a[100] = {};
    std::strstream str;
    str.rdbuf()->pubsetbuf(a, sizeof a);
    str << "Test string" << std::ends;
    std::cout << "user-provided buffer holds \"" << a << "\"\n";
}

✨ Possible output ✨

user-provided buffer holds "Test string"

🫢 Defect reports 🫢

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR LWG 66 β€” applied to C++98; behavior as published: the effect of setbuf() was "performs an operation that is defined separately for each class derived from strstreambuf", but there are no classes derived from strstreambuf; correct behavior: the effect is implementation-defined.

πŸ”₯ See also πŸ”₯

  • pubsetbuf β€” invokes setbuf() (public member function of std::basic_streambuf<CharT,Traits>)
  • setbuf [virtual] β€” replaces the buffer with user-defined array, if permitted (virtual protected member function of std::basic_streambuf<CharT,Traits>)
  • setbuf [virtual] β€” attempts to replace the controlled character sequence with an array (virtual protected member function of std::basic_stringbuf<CharT,Traits,Allocator>)
  • setbuf [virtual] β€” provides user-supplied buffer or turns this filebuf unbuffered (virtual protected member function of std::basic_filebuf<CharT,Traits>)

mwah πŸ’‹ this lil page came from cppreference.com, dated 2024.06.10, and it's all about std::strstreambuf::setbuf(3) β€” periodt. 🫢✨

πŸ“¦ where this page comes from