std::swap(s...sic_string)(3)
π
std::swap(std::basic_string)
okay so this page is from the C++ Standard Libary (their spelling, not mine, and honestly the confidence is iconic π). The title is std::swap(std::basic_string), and the NAME section is literally just that twice. Repetitive queen behavior, we love it.
π NAME
std::swap(std::basic_string) - std::swap(std::basic_string)
β¨ Synopsis
Defined in header <string>.
Until C++17, it's giving:
template< class CharT, class Traits, class Alloc >
void swap( std::basic_string<CharT, Traits, Alloc>& lhs,
std::basic_string<CharT, Traits, Alloc>& rhs );
Since C++17, and constexpr since C++20, it's giving:
template< class CharT, class Traits, class Alloc >
void swap( std::basic_string<CharT, Traits, Alloc>& lhs,
std::basic_string<CharT, Traits, Alloc>& rhs )
noexcept(/* see below */);
This specializes the std::swap algorithm for std::basic_string. It swaps the contents of lhs and rhs. Simple. Clean. Slay.
Equivalent to lhs.swap(rhs).
π«Ά Parameters
lhs,rhsβ the strings whose contents get swapped. That's the whole assignment, bestie.
πΈ Return value
(none) β it gives you nothing back. No return value, no crumbs, just vibes.
π¦ Complexity
Constant. Constant time, periodt.
π₯ Exceptions
Since C++17, the noexcept specification is:
noexcept(noexcept(lhs.swap(rhs)))
π Example
Run this code, bestie:
// Run this code
#include <iostream>
#include <string>
int main()
{
std::string a = "AAA";
std::string b = "BBBB";
std::cout << "Before swap:\n"
"a = " << a << "\n"
"b = " << b << "\n\n";
std::swap(a, b);
std::cout << "After swap:\n"
"a = " << a << "\n"
"b = " << b << '\n';
}
π Output
It prints exactly this:
Before swap:
a = AAA
b = BBBB
After swap:
a = BBBB
b = AAA
π Defect reports
These behavior-changing defect reports got applied retroactively to previously published C++ standards, no cap:
LWG 2064β applied to C++11. Behavior as published: non-memberswapwasnoexceptand inconsistent with memberswap. Correct behavior:noexceptremoved.
π See also
swapβ swaps the contents (public member function).
β Category
* conditionally noexcept
okay that's the whole page bestie β¨ β source http://cppreference.com, version/date 2024.06.10, page std::swap(s...sic_string)(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