std::basic_ostream::tellp(3)

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

πŸ’… std::basic_ostream::tellp β€” C++ Standard Library, but make it make sense

okay so bestie, welcome to std::basic_ostream::tellp(3) from the C++ Standard Library β€” yes that's the whole manual name, we're being official today πŸŽ€. This one's a little function with big main character energy: it tells you where you are in your output stream. That's it. That's the vibe. Let's get into it ✨


πŸ“› NAME

std::basic_ostream::tellp β€” literally std::basic_ostream::tellp, no notes, the name ate πŸ’…


🌸 Synopsis

pos_type tellp();

So what does it actually do? It returns the output position indicator of the current associated streambuf object. Think of it as the little cursor bookmark of your output stream β€” where's the next character gonna land? This function knows πŸ”₯

It behaves as an UnformattedOutputFunction (except without actually performing output) β€” and heads up, that "(since C++11)" applies to that no-actual-output part. After constructing and checking the sentry object:

  • If fail()==true, it returns pos_type(-1).
  • Otherwise, it returns rdbuf()->pubseekoff(0, std::ios_base::cur, std::ios_base::out).

Yes the whole call is that long, no we're not abbreviating it, character for character bestie 🫢


πŸ™ˆ Parameters

  • (none) β€” she takes nothing, she gives everything. Periodt.

πŸ’– Return value

  • current output position indicator on success βœ…
  • pos_type(-1) if a failure occurs πŸ’€

That's the whole deal. Success = a real position, failure = that sad little -1.


πŸ¦‹ Example

the original page has one of those // Run this code buttons, and here's the code it runs:

#include <iostream>
#include <sstream>
int main()
{
    std::ostringstream s;
    std::cout << s.tellp() << '\n';
    s << 'h';
    std::cout << s.tellp() << '\n';
    s << "ello, world ";
    std::cout << s.tellp() << '\n';
    s << 3.14 << '\n';
    std::cout << s.tellp() << '\n' << s.str();
}

πŸ“ Output

0
1
13
18
hello, world 3.14

and honestly? watching the position tick up 0 β†’ 1 β†’ 13 β†’ 18 while the string quietly builds itself into hello, world 3.14 is lowkey iconic 🌈


πŸ‘‘ See also

  • seekp β€” sets the output position indicator (public member function)
  • tellg β€” returns the input position indicator (public member function of std::basic_istream<CharT,Traits>)
  • seekg β€” sets the input position indicator (public member function of std::basic_istream<CharT,Traits>)

like tellp's whole friend group fr fr πŸ’œ


aaaand that's the page! source is http://cppreference.com, version 2024.06.10, and this gorgeous little entry is std::basic_ostream::tellp(3) β€” go check your stream position bestie, ily, bye πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from