cli20 0.1.2
A C++20 typed-schema command-line parser
Loading...
Searching...
No Matches
cli::StringLiteral< N > Struct Template Reference

Compile-time string type usable as a non-type template parameter. More...

#include <string_literal.hh>

Public Member Functions

consteval StringLiteral (const char(&str)[N]) noexcept
 Constructs from a C string literal, copying all N characters.
consteval auto size () const noexcept -> std::size_t
 Returns the string length excluding the null terminator.
consteval auto data () const noexcept -> const char *
 Returns a raw pointer to the first character.
consteval auto view () const noexcept -> std::string_view
 Returns a std::string_view over the stored characters.
constexpr auto operator[] (std::size_t i) const noexcept -> char
 Returns the character at index i.
consteval auto operator== (const StringLiteral &) const noexcept -> bool=default
 Equality comparison (compares the underlying arrays).

Public Attributes

std::array< char, N > value {}
 Raw character storage (null-terminated, length N).

Detailed Description

template<std::size_t N>
struct cli::StringLiteral< N >

Compile-time string type usable as a non-type template parameter.

C++20 allows class-type NTTPs provided the type satisfies structural requirements. StringLiteral<N> wraps a null-terminated character array so that string names such as "verbose" can appear directly in template argument lists:

cli::Flag<"verbose", 'v'> verbose;
Arg< Name, ShortName, pack::set_true, nargs::none > Flag
A boolean flag option that stores true when present.
Definition argument.hh:537
Template Parameters
NTotal storage size including the null terminator.

Definition at line 24 of file string_literal.hh.

Constructor & Destructor Documentation

◆ StringLiteral()

template<std::size_t N>
cli::StringLiteral< N >::StringLiteral ( const char(&) str[N])
inlineconstevalnoexcept

Constructs from a C string literal, copying all N characters.

Parameters
strNull-terminated string literal of length N.

Definition at line 34 of file string_literal.hh.

Member Function Documentation

◆ data()

template<std::size_t N>
auto cli::StringLiteral< N >::data ( ) const -> const char *
inlinenodiscardconstevalnoexcept

Returns a raw pointer to the first character.

Returns
Pointer to the underlying character array.

Definition at line 54 of file string_literal.hh.

◆ operator==()

template<std::size_t N>
auto cli::StringLiteral< N >::operator== ( const StringLiteral< N > & ) const -> bool=default
constevaldefaultnoexcept

Equality comparison (compares the underlying arrays).

◆ operator[]()

template<std::size_t N>
auto cli::StringLiteral< N >::operator[] ( std::size_t i) const -> char
inlinenodiscardconstexprnoexcept

Returns the character at index i.

Parameters
iZero-based character index.
Returns
The character at position i.

Definition at line 77 of file string_literal.hh.

◆ size()

template<std::size_t N>
auto cli::StringLiteral< N >::size ( ) const -> std::size_t
inlinenodiscardconstevalnoexcept

Returns the string length excluding the null terminator.

Returns
N - 1.

Definition at line 44 of file string_literal.hh.

◆ view()

template<std::size_t N>
auto cli::StringLiteral< N >::view ( ) const -> std::string_view
inlinenodiscardconstevalnoexcept

Returns a std::string_view over the stored characters.

The returned view does not include the null terminator.

Returns
A string_view of length size().

Definition at line 66 of file string_literal.hh.

Member Data Documentation

◆ value

template<std::size_t N>
std::array<char, N> cli::StringLiteral< N >::value {}

Raw character storage (null-terminated, length N).

Definition at line 26 of file string_literal.hh.


The documentation for this struct was generated from the following file: