std::basic_...buf::setbuf(3)
π
std::basic_...buf::setbuf(3) β from the C++ Standard Libary manual, bestie π
okay so this is the glow-up of the C++ Standard Libary page for std::basic_...buf::setbuf(3) (and yes, βLibaryβ is literally how itβs spelled in the manual β we keep the lore π). Itβs giving βbuffer setup but make it virtual,β and we are keeping every single deet.
π NAME π
std::basic_streambuf::pubsetbuf,std::basic_streambuf::setbufβ and the description is, no cap, the exact same string again:std::basic_streambuf::pubsetbuf,std::basic_streambuf::setbuf.
β¨ Synopsis β¨
okay so hereβs the signature situation, bestie:
public:
(1)
basic_streambuf<CharT, Traits>* pubsetbuf( char_type* s,
std::streamsize n )
protected:
virtual basic_streambuf<CharT, Traits>* setbuf( char_type* s,
std::streamsize n (2)
)
- Calls
setbuf(s, n)of the most derived class. - The base class version of this function has no effect. The derived classes may override this function to allow removal or replacement of the controlled character sequence (the buffer) with a user-provided array, or for any other implementation-specific purpose.
π«Ά Parameters π«Ά
sβ pointer to the firstCharTin the user-provided buffernβ the number ofCharTelements in the user-provided buffer
π Return value π
- The return value of
setbuf(s, n). this
πΈ Example πΈ
Provides a 10k buffer for reading. On linux, the strace utility may be used to observe the actual number of bytes read.
// Run this code
#include <fstream>
#include <iostream>
#include <string>
int main()
{
int cnt = 0;
std::ifstream file;
char buf[1024 * 10 + 1];
file.rdbuf()->pubsetbuf(buf, sizeof buf);
file.open("/usr/share/dict/words");
for (std::string line; getline(file, line);)
++cnt;
std::cout << cnt << '\n';
}
π¬ Possible output π¬
356010
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 158 - Applied to:
C++98 - Behavior as published: the default behavior of
setbufwas only specified as no-op - Correct behavior: specified for all cases if
gptr()is not null and not equal toegptr()
π See also π
setbufβ attempts to replace the controlled character sequence with an array[virtual](virtual protected member function ofstd::basic_stringbuf<CharT,Traits,Allocator>)setbufβ provides user-supplied buffer or turns thisfilebufunbuffered[virtual](virtual protected member function ofstd::basic_filebuf<CharT,Traits>)setbufβ attempts to replace the controlled character sequence with an array[virtual](virtual protected member function ofstd::strstreambuf)setbufβ sets the buffer for a file stream (function)
source/version + date, bb: http://cppreference.com, 2024.06.10, std::basic_...buf::setbuf(3) β mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms