Mundy: Multibody Nonlocal Dynamics Version of the Day
Loading...
Searching...
No Matches
mundy::StringLiteral< StrSize > Struct Template Reference

Literal class type that wraps a constant expression string. More...

#include <StringLiteral.hpp>

Inheritance diagram for mundy::StringLiteral< StrSize >:
[legend]

Public Member Functions

constexpr StringLiteral ()
 Default constructor that initializes the string literal to an empty string.
constexpr StringLiteral (const char(&str)[StrSize])
 Constructor that copies the string literal into the struct.
std::string to_string () const
 Convert the string literal to a std::string.
template<size_t OtherStrSize>
constexpr StringLiteral< StrSize+OtherStrSize - 1 > operator+ (const StringLiteral< OtherStrSize > &rhs) const
 Compile-time concatenation with another StringLiteral.
template<size_t OtherStrSize>
constexpr StringLiteral< StrSize+OtherStrSize - 1 > operator+ (const char(&rhs)[OtherStrSize]) const
 Compile-time concatenation with a char array.

Public Attributes

char value [StrSize]
 The string literal's content.

Static Public Attributes

static constexpr size_t size = StrSize
 The string literal's size.

Detailed Description

template<size_t StrSize>
struct mundy::StringLiteral< StrSize >

The StringLiteral struct allows templates to accept constant strings, effectively turning them into compile-time constants. This can result in performance benefits as computations using these strings can be performed at compile time.

How It Works: The constexpr keyword is used to ensure that all computations involving StringLiteral are done at compile time. The StringLiteral struct takes in the string and its size as template parameters and stores them. The string can then be accessed with the value member, and the size with the size member.

Example Usage:

template <StringLiteral StrLit>
void Print() {
// The size of the string is available as a constant expression.
constexpr auto size = StrLit.size;
// and so is the string's content.
constexpr auto contents = StrLit.value;
std::cout << "Size: " << size << ", Contents: " << contents << std::endl;
}
int main() {
Print<make_string_literal("literal string")>(); // Prints "Size: 15, Contents: literal string"
}
constexpr StringLiteral< N > make_string_literal(const char(&str)[N])
Helper function for creating a StringLiteral.
Definition StringLiteral.hpp:174
static constexpr size_t size
The string literal's size.
Definition StringLiteral.hpp:99

Credit where credit is due: This design entirely originates from Kevin Hartman's Passing String Literals as Template Parameters in C++20 blog post: https://ctrpeach.io/posts/cpp20-string-literal-template-parameters/#:~:text=This%20would%20work%20by%20wrapping,e.g.%20char%5BN%5D%20).

Template Parameters
StrSizeThe size of the string literal

Constructor & Destructor Documentation

◆ StringLiteral() [1/2]

template<size_t StrSize>
mundy::StringLiteral< StrSize >::StringLiteral ( )
inlineconstexpr

◆ StringLiteral() [2/2]

template<size_t StrSize>
mundy::StringLiteral< StrSize >::StringLiteral ( const char(&) str[StrSize])
inlineexplicitconstexpr
Parameters
strThe string literal to copy

Member Function Documentation

◆ to_string()

template<size_t StrSize>
std::string mundy::StringLiteral< StrSize >::to_string ( ) const
inline

◆ operator+() [1/2]

template<size_t StrSize>
template<size_t OtherStrSize>
StringLiteral< StrSize+OtherStrSize - 1 > mundy::StringLiteral< StrSize >::operator+ ( const StringLiteral< OtherStrSize > & rhs) const
inlineconstexpr
Parameters
rhsThe right-hand side of the concatenation
Returns
A new StringLiteral with the combined content of the two input StringLiterals

◆ operator+() [2/2]

template<size_t StrSize>
template<size_t OtherStrSize>
StringLiteral< StrSize+OtherStrSize - 1 > mundy::StringLiteral< StrSize >::operator+ ( const char(&) rhs[OtherStrSize]) const
inlineconstexpr
Parameters
rhsThe right-hand side of the concatenation
Returns
A new StringLiteral with the combined content of the input StringLiteral and char array

Member Data Documentation

◆ value

template<size_t StrSize>
char mundy::StringLiteral< StrSize >::value[StrSize]

◆ size

template<size_t StrSize>
size_t mundy::StringLiteral< StrSize >::size = StrSize
staticconstexpr