JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
Loading...
Searching...
No Matches
t_str.h File Reference

A C++20 compile-time string wrapper enabling string literals as non-type template parameters (NTTP). More...

#include <utility>
#include <string>
#include <string_view>
#include <cstdint>
#include "jh/pods/array.h"
#include "jh/pods/string_view.h"
#include "jh/metax/hash.h"
#include "jh/macros/platform.h"

Go to the source code of this file.

Classes

struct  jh::meta::t_str< N >
 Compile-time string wrapper for use as a non-type template parameter (NTTP). More...

Namespaces

namespace  jh::meta
 Aggregated entry point for compile-time metaprogramming utilities.

Typedefs

template<std::uint16_t N>
using jh::meta::TStr = t_str<N>
 Alias for t_str<N> with template argument deduction.

Functions

template<std::uint16_t N>
std::ostream & jh::meta::operator<< (std::ostream &os, const t_str< N > &str)
 Stream output operator for t_str<N>.

Detailed Description

A C++20 compile-time string wrapper enabling string literals as non-type template parameters (NTTP).

Author
JeongHan-Bae <mastropseudo@gmail.com>

t_str<N> is a compile-time string container that enables C++20 non-type template parameters (NTTP) with string literals.

Motivation

Before C++20, it was not possible to bind string literals directly as template arguments. If a type needed an associated string-based metadata, developers had to:

  • Define a base class with a virtual/static metadata function,
  • Override the metadata in every derived class,
  • Access it indirectly during template instantiation.

With t_str<N> and the TStr alias, this indirection is no longer needed: string literals can be passed directly as template parameters and validated at compile time.

Key Advantages

  • Direct NTTP binding: use t_str to inject string literals directly into templates.
  • Compile-time validation: check properties such as digit, alnum, hex, base64, and ASCII legality.
  • Compile-time transformation: supports to_upper(), to_lower(), and flip_case().
  • Concatenation with limits: safe constexpr concatenation with a maximum total size (default 16 KB).
  • Zero runtime overhead: no dynamic allocation, everything is constexpr.

Design Notes

  • The maximum supported string size is 16 KB.
  • All strings are null-terminated (N - 1 is the effective length).
  • Intended exclusively for string literals in template contexts.
  • Not a runtime replacement for std::string or immutable_str.
Version
1.4.x
Date
2025