std::basic_...ng::max_size(3)
πΈ NAME πΈ
std::basic_string::max_size β literally std::basic_string::max_size, bestie. It's giving self-titled main character energy β¨. This page is from the C++ Standard Libary manual (yes, "Libary" with the typo in the header, I'm just reporting the vibes π), and the header folds it in as std::basic_...ng::max_size(3) because truncation ate the middle of the word and left us with a mystery π.
π¦ Synopsis π¦
Here's the signature, no cap:
size_type max_size() const; (noexcept since C++11)
(constexpr since C++20)
It returns the maximum number of elements the string is able to hold due to system or library implementation limitations, i.e. std::distance(begin(), end()) for the largest string. That's the whole assignment, periodt.
β¨ Parameters β¨
(none) β no parameters. Zero. Zilch. Nada. π«Ά
π Return value π
Maximum number of characters. That's it. That's the tea. π
π₯ Complexity π₯
Constant. Literally constant. No cap.
π Example π
The original page has a literal // Run this code note above the block, and then this exact example code (yes, the line wraps are chaotic β I did not come here to fix cppreference's formatting, I came to serve facts π
):
#include <array>
#include <climits>
#include <iomanip>
#include <iostream>
#include <locale>
#include <string>
#include <typeinfo>
#include <boost/core/demangle.hpp>
template<typename T>
void print_basic_string_max_size()
{
std::basic_string<T> s;
auto max_size = s.max_size();
std::cout.imbue(std::locale("en_US.UTF-8"));
std::cout << "basic_string<" <<
boost::core::demangle(typeid(T).name())
<< ">:\n\t" << max_size << " = ";
std::cout << std::setprecision(3) << (double) max_size << " = ";
std::cout.imbue(std::locale("C"));
std::cout << std::hex << std::setfill('0') << "0x"
<< std::setw(sizeof(typename decltype(s)::size_type) <<
1)
<< max_size << '\n' << std::dec;
};
int main()
{
std::cout << "Pointer size: " << CHAR_BIT * sizeof(void*) << "
bits\n"
"Maximum sizes:\n";
print_basic_string_max_size<char>();
print_basic_string_max_size<char16_t>();
print_basic_string_max_size<char32_t>();
print_basic_string_max_size<wchar_t>();
print_basic_string_max_size<long>();
using CharT = std::array<char, 01232>;
print_basic_string_max_size<CharT>();
}
Possible output:
Pointer size: 64 bits
Maximum sizes:
basic_string<char>:
9,223,372,036,854,775,807 = 9.22e+18 = 0x7fffffffffffffff
basic_string<char16_t>:
4,611,686,018,427,387,903 = 4.61e+18 = 0x3fffffffffffffff
basic_string<char32_t>:
2,305,843,009,213,693,951 = 2.31e+18 = 0x1fffffffffffffff
basic_string<wchar_t>:
2,305,843,009,213,693,951 = 2.31e+18 = 0x1fffffffffffffff
basic_string<long>:
1,152,921,504,606,846,975 = 1.15e+18 = 0x0fffffffffffffff
basic_string<std::array<char, 666ul>>:
13,848,906,962,244,407 = 1.38e+16 = 0x00313381ec031337
π See also π
sizeβ returns the number of characterslengthβ (public member function)max_sizeβ returns the maximum number of characters (public member function ofstd::basic_string_view<CharT,Traits>)
xoxo, your Unix bestie π
β source/version/date: http://cppreference.com, 2024.06.10, page std::basic_...ng::max_size(3). periodt. π
π¦ 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