std::basic_...ng::pop_back(3)

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

std::basic_...ng::pop_back(3) πŸ’…

okay so this page is from the C++ Standard Libary manual β€” yes, that's the exact spelling on the tin, bestie, we are not fixing it πŸ’…. It's giving β€œtiny member function, big main-character energy.”


🌸 NAME 🌸

std::basic_string::pop_back - std::basic_string::pop_back

Literally the name on the label, no notes. It's the pop_back situation for std::basic_string, and we're obsessed πŸŽ€.

πŸ¦‹ Synopsis πŸ¦‹

void pop_back();  (constexpr since C++20)

It removes the last character from the string. It's equivalent to erase(end() - 1).

🚨 Warning, bestie: the behavior is undefined if the string is empty. Not β€œoops,” not β€œmaybe fine” β€” undefined. Do not test it. Do not poke it. Do not send it a text. πŸ’€

πŸ“ Parameters πŸ“

(none)

Bestie, there are zero parameters. None. Nada. The function said β€œI need nothing from you” and honestly? Iconic. ✨

πŸŽ€ Return value πŸŽ€

(none)

It returns nothing. It simply does the thing and leaves. No receipts, no follow-up. 🫢

πŸ”₯ Complexity πŸ”₯

Constant.

Constant time, periodt. ⭐

🌈 Exceptions 🌈

Throws nothing.

Good news, bestie: it throws nothing. That's the whole sentence. Slay. πŸ’…

🧚 Notes 🧚

In libstdc++, pop_back() is not available in C++98 mode.

So like… if you're in C++98 mode, this function is not available. That's the note. That's the vibe. πŸ™ˆ

πŸ’œ Example πŸ’œ

// Run this code

#include <cassert>
#include <iomanip>
#include <iostream>
#include <string>

int main()
{
    std::string str("Short string!");
    std::cout << "before=" << std::quoted(str) << '\n';
    assert(str.size() == 13);

    str.pop_back();
    std::cout << " after=" << std::quoted(str) << '\n';
    assert(str.size() == 12);

    str.clear();
//  str.pop_back(); // undefined behavior
}

🌟 Output: 🌟

before="Short string!"
 after="Short string"

Notice the commented-out str.pop_back(); after str.clear();? That's the undefined behavior warning living right there in the code, bestie. We respect the comment. We do not run the comment. 😭

πŸŽ€ Defect reports πŸŽ€

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

  • DR: LWG 534 β€” Applied to: C++98 β€” Behavior as published: std::basic_string did not have the member function pop_back() β€” Correct behavior: added.

So yeah, LWG 534 walked in retroactively and said β€œactually, let's add that member function.” History was made. πŸ‘‘

πŸ¦‹ See also πŸ¦‹

  • push_back β€” appends a character to the end (public member function)
  • erase β€” removes characters (public member function)

🫢 that's the whole page, bestie β€” sourced from http://cppreference.com, dated 2024.06.10, and filed under std::basic_...ng::pop_back(3). periodt. πŸŽ€

πŸ“¦ where this page comes from