cli20 0.1.2
A C++20 typed-schema command-line parser
Loading...
Searching...
No Matches
string_literal.hh
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <string_view>
6
7namespace cli {
8
23template <std::size_t N>
26 std::array<char, N> value{};
27
33 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
34 consteval StringLiteral(const char (&str)[N]) noexcept {
35 std::copy_n(str, N, value.begin());
36 }
37
43 [[nodiscard]]
44 consteval auto size() const noexcept -> std::size_t {
45 return N - 1;
46 }
47
53 [[nodiscard]]
54 consteval auto data() const noexcept -> const char* {
55 return value.data();
56 }
57
65 [[nodiscard]]
66 consteval auto view() const noexcept -> std::string_view {
67 return {value.data(), size()};
68 }
69
76 [[nodiscard]]
77 constexpr auto operator[](std::size_t i) const noexcept -> char {
78 return value[i];
79 }
80
84 consteval auto operator==(const StringLiteral&) const noexcept
85 -> bool = default;
86};
87
91template <std::size_t N>
92// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
93StringLiteral(const char (&)[N]) -> StringLiteral<N>;
94
95} // namespace cli
Definition action.hh:29
StringLiteral(const char(&)[N]) -> StringLiteral< N >
Deduction guide: StringLiteral("hello") deduces StringLiteral<6>.
Compile-time string type usable as a non-type template parameter.
consteval auto size() const noexcept -> std::size_t
Returns the string length excluding the null terminator.
consteval auto operator==(const StringLiteral &) const noexcept -> bool=default
Equality comparison (compares the underlying arrays).
consteval auto view() const noexcept -> std::string_view
Returns a std::string_view over the stored characters.
consteval auto data() const noexcept -> const char *
Returns a raw pointer to the first character.
std::array< char, N > value
Raw character storage (null-terminated, length N).
consteval StringLiteral(const char(&str)[N]) noexcept
Constructs from a C string literal, copying all N characters.
constexpr auto operator[](std::size_t i) const noexcept -> char
Returns the character at index i.