template <typename Character, size_t Length>
muu::fixed_string class

A null-terminated string for compile-time string manipulation.

Template parameters
Character The character type of the string.
Length The length of the string (not including the null terminator).

Public types

using const_iterator = const_pointer
Iterator type used when iterating over the string.
using const_pointer = const Character*
Constant pointer to one of the string's characters.
using const_reference = const Character&
Constant reference to one of the string's characters.
using const_reverse_iterator = std::reverse_iterator<const_iterator>
Iterator type used when reverse-iterating over the string.
using difference_type = ptrdiff_t
Difference type used by iterators.
using iterator = pointer
Iterator type used when iterating over the string.
using pointer = Character*
Pointer to one of the string's characters.
using reference = Character&
Reference to one of the string's characters.
using reverse_iterator = std::reverse_iterator<iterator>
Iterator type used when reverse-iterating over the string.
using size_type = size_t
Size/index type.
using traits_type = std::char_traits<Character>
Traits type describing the string's character type.
using value_type = Character
Character type used in the string.
using view_type = std::basic_string_view<Character>
The string view type corresponding to this string's character type.

Public static variables

static size_t string_length constexpr
The length of the string (not including the null-terminator).

Constructors, destructors, conversion operators

fixed_string() defaulted noexcept
Default constructor. Characters are not initialized.
fixed_string(const fixed_string&) defaulted constexpr noexcept
Copy constructor.
template <size_t N>
fixed_string(const value_type(&arr)[N]) explicit constexpr noexcept
Constructs a string from a character array.
fixed_string(std::string_view str) explicit constexpr noexcept
Constructs a string from a std::string_view.
template <size_t Len>
fixed_string(const fixed_string<value_type, Len>& str) explicit constexpr noexcept
Lengthening/truncating constructor.
fixed_string(value_type fill) explicit constexpr noexcept
Constructs a string with each character equal to the given value.
template <size_t Len>
fixed_string(const value_type* str, index_tag<Len>) explicit constexpr noexcept
Constructs a string from a raw c-string pointer and integral length constant.

Public functions

auto operator=(const fixed_string&) -> fixed_string& defaulted constexpr noexcept
Copy-assigment operator.

Characters

auto back() -> reference constexpr noexcept
Returns a reference to the last character in the string.
auto back() const -> const_reference constexpr noexcept
Returns a const reference to the last character in the string.
auto c_str() const -> const_pointer constexpr noexcept
Returns a const pointer to the first character in the string.
auto data() -> pointer constexpr noexcept
Returns a pointer to the first character in the string.
auto data() const -> const_pointer constexpr noexcept
Returns a const pointer to the first character in the string.
auto front() -> reference constexpr noexcept
Returns a reference to the first character in the string.
auto front() const -> const_reference constexpr noexcept
Returns a const reference to the first character in the string.
template <size_t Index>
auto get() -> reference constexpr noexcept
Returns a reference to the character at the given index.
template <size_t Index>
auto get() const -> const_reference constexpr noexcept
Returns a const reference to the character at the given index.
auto operator[](size_type idx) -> reference constexpr noexcept
Returns a reference to the character at the given index.
auto operator[](size_type idx) const -> const_reference constexpr noexcept
Returns a const reference to the character at the given index.

Concatenation

template <size_t Len>
auto operator+(const fixed_string& lhs, const fixed_string<value_type, Len>& rhs) -> fixed_string<value_type, Length+Len> constexpr noexcept
Concatenates two static strings.
auto operator+(const fixed_string& lhs, value_type rhs) -> fixed_string<value_type, Length+1> constexpr noexcept
Concatenates a static string and a single character.
auto operator+(value_type lhs, const fixed_string& rhs) -> fixed_string<value_type, Length+1> constexpr noexcept
Concatenates a static string and a single character.

Equality and Comparison

template <size_t Len>
static auto compare(const fixed_string& lhs, const fixed_string<value_type, Len>& rhs) -> int constexpr noexcept
Returns the lexicographical ordering of two strings.
template <size_t Len>
auto compare(const fixed_string<value_type, Len>& rhs) -> int constexpr noexcept
Returns the lexicographical ordering of this string with respect to another.
template <size_t Len>
auto operator!=(const fixed_string& lhs, const fixed_string<value_type, Len>& rhs) -> bool constexpr noexcept
Returns true if two strings do not contain the same character sequence.
template <size_t Len>
auto operator<(const fixed_string& lhs, const fixed_string<value_type, Len>& rhs) -> bool constexpr noexcept
Returns true if the LHS is lexicographically ordered before the RHS.
template <size_t Len>
auto operator<=(const fixed_string& lhs, const fixed_string<value_type, Len>& rhs) -> bool constexpr noexcept
Returns true if the LHS is lexicographically ordered before or equal to the RHS.
template <size_t Len>
auto operator==(const fixed_string& lhs, const fixed_string<value_type, Len>& rhs) -> bool constexpr noexcept
Returns true if two strings contain the same character sequence.
template <size_t Len>
auto operator>(const fixed_string& lhs, const fixed_string<value_type, Len>& rhs) -> bool constexpr noexcept
Returns true if the LHS is lexicographically ordered after than the RHS.
template <size_t Len>
auto operator>=(const fixed_string& lhs, const fixed_string<value_type, Len>& rhs) -> bool constexpr noexcept
Returns true if the LHS is lexicographically ordered after or equal to the RHS.

Iterators

auto begin() -> iterator constexpr noexcept
Returns an iterator to the first character in the string.
auto begin() const -> const_iterator constexpr noexcept
Returns a const iterator to the first character in the string.
auto cbegin() const -> const_iterator constexpr noexcept
Returns a const iterator to the first character in the string.
auto cend() const -> const_iterator constexpr noexcept
Returns a const iterator to the one-past-the-last character in the string.
auto crbegin() const -> const_reverse_iterator constexpr noexcept
Returns a const reverse iterator to the last character in the string.
auto crend() const -> const_reverse_iterator constexpr noexcept
Returns a const reverse iterator to one-before-the-first character in the string.
auto end() -> iterator constexpr noexcept
Returns an iterator to the one-past-the-last character in the string.
auto end() const -> const_iterator constexpr noexcept
Returns a const iterator to the one-past-the-last character in the string.
auto rbegin() -> reverse_iterator constexpr noexcept
Returns a reverse iterator to the last character in the string.
auto rbegin() const -> const_reverse_iterator constexpr noexcept
Returns a const reverse iterator to the last character in the string.
auto rend() -> reverse_iterator constexpr noexcept
Returns a reverse iterator to one-before-the-first character in the string.
auto rend() const -> const_reverse_iterator constexpr noexcept
Returns a const reverse iterator to one-before-the-first character in the string.

Iterators (ADL)

auto begin(fixed_string& s) -> iterator constexpr noexcept
Returns an iterator to the first character in a string.
auto begin(const fixed_string& s) -> const_iterator constexpr noexcept
Returns a const iterator to the first character in a string.
auto cbegin(const fixed_string& s) -> const_iterator constexpr noexcept
Returns a const iterator to the first character in a string.
auto cend(const fixed_string& s) -> const_iterator constexpr noexcept
Returns a const iterator to the one-past-the-last character in a string.
auto crbegin(const fixed_string& s) -> const_reverse_iterator constexpr noexcept
Returns a const reverse iterator to the last character in a string.
auto crend(const fixed_string& s) -> const_reverse_iterator constexpr noexcept
Returns a const reverse iterator to one-before-the-first character in a string.
auto end(fixed_string& s) -> iterator constexpr noexcept
Returns an iterator to the one-past-the-last character in a string.
auto end(const fixed_string& s) -> const_iterator constexpr noexcept
Returns a const iterator to the one-past-the-last character in a string.
auto rbegin(fixed_string& s) -> reverse_iterator constexpr noexcept
Returns a reverse iterator to the last character in a string.
auto rbegin(const fixed_string& s) -> const_reverse_iterator constexpr noexcept
Returns a const reverse iterator to the last character in a string.
auto rend(fixed_string& s) -> reverse_iterator constexpr noexcept
Returns a reverse iterator to one-before-the-first character in a string.
auto rend(const fixed_string& s) -> const_reverse_iterator constexpr noexcept
Returns a const reverse iterator to one-before-the-first character in a string.

Length

auto empty() const -> bool constexpr noexcept
Returns true if the string is empty.
auto length() const -> size_type constexpr noexcept
Returns the number of characters in the string (not including the null-terminator).
operator bool() const explicit constexpr noexcept
Returns true if the string is not empty.
auto size() const -> size_type constexpr noexcept
Returns the number of characters in the string (not including the null-terminator).

Substrings

template <auto Start, auto End = static_cast<size_t>(-1)>
auto slice() const -> auto constexpr noexcept
Returns a substring.
template <auto Start, size_t Len = static_cast<size_t>(-1)>
auto substr() const -> auto constexpr noexcept
Returns a substring.

Views

operator view_type() const constexpr noexcept
Returns a view of the string (not including the null terminator).
auto view() const -> view_type constexpr noexcept
Returns a view of the string (not including the null terminator).

Friends

template <typename Char, typename Traits>
auto operator<<(std::basic_ostream<Char, Traits>& os, const fixed_string& str) -> std::basic_ostream<Char, Traits>&
Writes the string out to a text stream.

Function documentation

template <typename Character, size_t Length>
template <size_t N>
muu::fixed_string::fixed_string(const value_type(&arr)[N]) explicit constexpr noexcept

Constructs a string from a character array.

Any extra characters not covered by the input argument are zero-initialized.

template <typename Character, size_t Length>
muu::fixed_string::fixed_string(std::string_view str) explicit constexpr noexcept

Constructs a string from a std::string_view.

Any extra characters not covered by the input argument are zero-initialized.

template <typename Character, size_t Length>
template <size_t Len>
muu::fixed_string::fixed_string(const fixed_string<value_type, Len>& str) explicit constexpr noexcept

Lengthening/truncating constructor.

Any extra characters not covered by the input argument are zero-initialized.

template <typename Character, size_t Length>
reference muu::fixed_string::back() constexpr noexcept

Returns a reference to the last character in the string.

template <typename Character, size_t Length>
const_reference muu::fixed_string::back() const constexpr noexcept

Returns a const reference to the last character in the string.

template <typename Character, size_t Length>
reference muu::fixed_string::front() constexpr noexcept

Returns a reference to the first character in the string.

template <typename Character, size_t Length>
const_reference muu::fixed_string::front() const constexpr noexcept

Returns a const reference to the first character in the string.

template <typename Character, size_t Length>
template <auto Start, auto End = static_cast<size_t>(-1)>
auto muu::fixed_string::slice() const constexpr noexcept

Returns a substring.

Template parameters
Start Starting index of the substring, inclusive. Negative values mean "this many characters from the end".
End Ending index of the substring, exclusive. Negative values mean "this many characters from the end".
Returns A string containing the substring from the given range. If the source string literal was empty, the range was empty, or they do not intersect, an zero-length string is returned.

template <typename Character, size_t Length>
template <auto Start, size_t Len = static_cast<size_t>(-1)>
auto muu::fixed_string::substr() const constexpr noexcept

Returns a substring.

Template parameters
Start Starting index of the substring. Negative values mean "this many characters from the end".
Len Length of the substring.
Returns A string containing the substring from the given range. If the source string literal was empty, the range was empty, or they do not intersect, an zero-length string literal is returned.

template <typename Character, size_t Length>
template <impl::fixed_string_udl_impl Str>
auto operator""_fs() consteval

Constructs a fixed_string directly using a string literal.

constexpr auto str = "hello"_fs;
static_assert(str == fixed_string{ "hello" })