Qx v0.7
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::StringLiteral< C, N > Class Template Reference

The StringLiteral template class acts as a literal class type wrapper around a raw string that effectively allows the string to be used as a non-type template parameter. More...

#include <qx/utility/qx-stringliteral.h>

Public Types

using data_t = C
 
using view_t
 

Public Member Functions

constexpr StringLiteral (const C(&str)[N])
 
constexpr C * data () const
 
constexpr operator QLatin1StringView () const
 
constexpr operator QStringView () const
 
constexpr operator std::basic_string_view< C > () const
 
constexpr std::strong_ordering operator<=> (const StringLiteral &other) const =default
 
constexpr bool operator== (const StringLiteral &other) const =default
 
constexpr size_t size () const
 
constexpr std::basic_string_view< C > std_view () const
 
constexpr view_t view () const
 

Static Public Attributes

static constexpr size_t size_v = N - 1
 

Related Symbols

(Note that these are not member symbols.)

template<string_literal S, size_t N2>
constexpr auto operator+ (const S &a, const typename S::data_t(&b)[N2])
 
template<string_literal S>
constexpr auto operator+ (const S &a, typename S::data_t b)
 
template<typename StringLiteralA, typename StringLiteralB>
constexpr auto operator+ (const StringLiteralA &a, const StringLiteralA &b)
 
template<size_t N1, string_literal S>
constexpr auto operator+ (const typename S::data_t(&a)[N1], const S &b)
 
template<string_literal S>
constexpr auto operator+ (typename S::data_t a, const S &b)
 

Detailed Description

template<std::integral C, size_t N>
class Qx::StringLiteral< C, N >

The template parameters of this class allow the underlying character type to be auto-detected by the nature of the literal string used to construct it, so a template that takes Qx::StringLiteral can accept multiple string types, as shown in the following example.

#include <qx/utility/qx-stringliteral.h>
#include <iostream>
template<Qx::StringLiteral S>
void printString()
{
if constexpr(std::same_as<typename decltype(S)::data_t, char>)
std::cout << "The string is: " << S.std_view();
else if constexpr(std::same_as<typename decltype(S)::data_t, wchar_t>)
std::wcout << "The string is: " << S.std_view();
}
int main()
{
printString<"Hello">();
printString<L"World">();
return 0;
}

If you want to use compile-time literals of a fixed size, use the type-specific derivatives of this class (i.e CStringLiteral, WStringLiteral, U8StringLiteral, etc.)

Member Typedef Documentation

◆ data_t

template<std::integral C, size_t N>
using Qx::StringLiteral< C, N >::data_t = C

The storage type for the string.

◆ view_t

template<std::integral C, size_t N>
using Qx::StringLiteral< C, N >::view_t
Initial value:
std::conditional_t<
std::is_same_v<C, char>,
QLatin1StringView,
std::conditional_t<
std::is_same_v<C, char16_t>,
QStringView,
void
>
>

The type of view used by the view() method, if any.

Constructor & Destructor Documentation

◆ StringLiteral()

template<std::integral C, size_t N>
Qx::StringLiteral< C, N >::StringLiteral ( const C(&) str[N])
inlineconstexpr

Wraps the raw string str of length N.

Warning
If preparing a character array manually, be sure to included a null-terminator as its presence is assumed! If none is provided, the "size" members will report incorrect values and concatenation will result in UB.

Member Function Documentation

◆ data()

template<std::integral C, size_t N>
C * Qx::StringLiteral< C, N >::data ( ) const
inlineconstexpr

Returns a pointer to the raw string.

◆ operator QLatin1StringView()

template<std::integral C, size_t N>
Qx::StringLiteral< C, N >::operator QLatin1StringView ( ) const
inlineconstexpr

Allows the StringLiteral to be converted to a QLatin1StringView.

Requires C to be char.

◆ operator QStringView()

template<std::integral C, size_t N>
Qx::StringLiteral< C, N >::operator QStringView ( ) const
inlineconstexpr

Allows the StringLiteral to be converted to a QLatin1StringView.

Requires C to be char16_t.

◆ operator std::basic_string_view< C >()

template<std::integral C, size_t N>
Qx::StringLiteral< C, N >::operator std::basic_string_view< C > ( ) const
inlineconstexpr

Allows the StringLiteral to be converted to a std::basic_string_view of the appropriate type.

◆ operator<=>()

template<std::integral C, size_t N>
std::strong_ordering Qx::StringLiteral< C, N >::operator<=> ( const StringLiteral< C, N > & other) const
constexprdefault

Returns the ordering of other relative to this.

◆ operator==()

template<std::integral C, size_t N>
bool Qx::StringLiteral< C, N >::operator== ( const StringLiteral< C, N > & other) const
constexprdefault

Returns true if the StringLiteral is equal to other; otherwise, returns false.

◆ size()

template<std::integral C, size_t N>
size_t Qx::StringLiteral< C, N >::size ( ) const
inlineconstexpr

Returns the size of the string.

◆ std_view()

template<std::integral C, size_t N>
std::basic_string_view< C > Qx::StringLiteral< C, N >::std_view ( ) const
inlineconstexpr

Returns a std style view of the string.

◆ view()

template<std::integral C, size_t N>
view_t Qx::StringLiteral< C, N >::view ( ) const
inlineconstexpr

Returns a Qt type view of the string.

Requires C to be char or char16_t

Friends And Related Symbol Documentation

◆ operator+() [1/5]

template<string_literal S, size_t N2>
auto operator+ ( const S & a,
const typename S::data_t(&) b[N2] )
related

Returns a string which is the result of concatenating a and b.

◆ operator+() [2/5]

template<string_literal S>
auto operator+ ( const S & a,
typename S::data_t b )
related

Returns a string which is the result of concatenating a and b.

◆ operator+() [3/5]

template<typename StringLiteralA, typename StringLiteralB>
auto operator+ ( const StringLiteralA & a,
const StringLiteralA & b )
related

Returns a string which is the result of concatenating a and b.

◆ operator+() [4/5]

template<size_t N1, string_literal S>
auto operator+ ( const typename S::data_t(&) a[N1],
const S & b )
related

Returns a string which is the result of concatenating a and b.

◆ operator+() [5/5]

template<string_literal S>
auto operator+ ( typename S::data_t a,
const S & b )
related

Returns a string which is the result of concatenating a and b.

Member Data Documentation

◆ size_v

template<std::integral C, size_t N>
size_t Qx::StringLiteral< C, N >::size_v = N - 1
staticconstexpr

The size of the string.

This variable is easy to use at compile-time given it's a static member.


The documentation for this class was generated from the following files: