|
| 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.
|
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() {
constexpr auto size = StrLit.size;
constexpr auto contents = StrLit.value;
std::cout <<
"Size: " <<
size <<
", Contents: " << contents << std::endl;
}
int main() {
}
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
-
| StrSize | The size of the string literal |