Skip to main content

std::string_view max_size() method

// Const version only
constexpr bool max_size() const noexcept;

Returns the maximum number of characters the view is able to hold due to system or library implementation limitations, i.e. std::distance(begin(), end()) for the largest container.

Parameters

(none)

Return value

Maximum number of characters the view can hold.

Complexity

Constant - O(1).

Notes

This value typically reflects the theoretical limit on the size of the container, at most std::numeric_limits<difference_type>::max().

At runtime, the size of the container may be limited to a value smaller than max_size() by the amount of RAM available.

Example

Main.cpp
#include <iostream>
#include <limits>
#include <string_view>

int main() {
std::cout
<< std::numeric_limits<std::int64_t>::max()
<< " <- numeric_limits<int64_t>::max()\n"
<< std::string_view{}.max_size()
<< " <- std::string_view::max_size()\n"
<< std::basic_string_view<char>{}.max_size()
<< " <- std::basic_string_view<char>::max_size()\n"
<< std::basic_string_view<char16_t>{}.max_size()
<< " <- std::basic_string_view<char16_t>::max_size()\n"
<< std::wstring_view{}.max_size()
<< " <- std::wstring_view::max_size()\n"
<< std::basic_string_view<char32_t>{}.max_size()
<< " <- std::basic_string_view<char32_t>::max_size()\n"
;
}
Possible output
9223372036854775807 <- numeric_limits<int64_t>::max()
4611686018427387899 <- std::string_view::max_size()
4611686018427387899 <- std::basic_string_view<char>::max_size()
2305843009213693949 <- std::basic_string_view<char16_t>::max_size()
1152921504606846974 <- std::wstring_view::max_size()
1152921504606846974 <- std::basic_string_view<char32_t>::max_size()
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string_view max_size() method

// Const version only
constexpr bool max_size() const noexcept;

Returns the maximum number of characters the view is able to hold due to system or library implementation limitations, i.e. std::distance(begin(), end()) for the largest container.

Parameters

(none)

Return value

Maximum number of characters the view can hold.

Complexity

Constant - O(1).

Notes

This value typically reflects the theoretical limit on the size of the container, at most std::numeric_limits<difference_type>::max().

At runtime, the size of the container may be limited to a value smaller than max_size() by the amount of RAM available.

Example

Main.cpp
#include <iostream>
#include <limits>
#include <string_view>

int main() {
std::cout
<< std::numeric_limits<std::int64_t>::max()
<< " <- numeric_limits<int64_t>::max()\n"
<< std::string_view{}.max_size()
<< " <- std::string_view::max_size()\n"
<< std::basic_string_view<char>{}.max_size()
<< " <- std::basic_string_view<char>::max_size()\n"
<< std::basic_string_view<char16_t>{}.max_size()
<< " <- std::basic_string_view<char16_t>::max_size()\n"
<< std::wstring_view{}.max_size()
<< " <- std::wstring_view::max_size()\n"
<< std::basic_string_view<char32_t>{}.max_size()
<< " <- std::basic_string_view<char32_t>::max_size()\n"
;
}
Possible output
9223372036854775807 <- numeric_limits<int64_t>::max()
4611686018427387899 <- std::string_view::max_size()
4611686018427387899 <- std::basic_string_view<char>::max_size()
2305843009213693949 <- std::basic_string_view<char16_t>::max_size()
1152921504606846974 <- std::wstring_view::max_size()
1152921504606846974 <- std::basic_string_view<char32_t>::max_size()
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.