"" "" [
](LICENSE) 
Overview
hybstr provides a hybrid string type (hybstr::string_impl) that seamlessly bridges compile-time (constexpr) and runtime string handling.
Unlike std::string (purely runtime) or std::string_view (non-owning), hybstr::string_impl owns its data and can operate in both environments.
In both compile-time and runtime enviroments, all data lives in a fixed buffer known to the compiler.
hybstr lets you treat strings as first-class compile-time objects — useful for metaprogramming, code generation, embedded DSLs, or constexpr evaluation — while still functioning as a normal string type at runtime.
Features
- Hybrid compile-time / runtime Works in both constexpr and regular runtime expressions.
- Concatenation and modification Supports operations like +, append, push_back, and set<N>() in both compile time and runtime.
- NTTP Can be used as a Non-Type Template Parameter (NTTP) in C++20 and above.
Examples
Construction
constexpr auto s1 =
string(
"Hello");
constexpr auto s2 =
string(
"World");
constexpr auto s3 = s1 + ", " + s2 + "!";
static_assert(s3 ==
string(
"Hello, World!"));
Hybrid string implementation that can be used in both compile time and runtime.
constexpr auto string()
Creates an empty hybrid string.
Definition hybstr.hpp:945
Literals
constexpr auto greet = "Hi"_hyb + ", "_hyb + "there"_hyb;
Contains user defined literal operators for creating 'hybstr::string_impl' objects.
From Runtime Inputs
std::string name;
std::cin >> name;
constexpr auto s1 =
string(
"Hello");
auto msg = s1 + name;
std::cout << msg.str() << '\n';
std::cout << msg.view() << '\n';
Compile time utils
C++20
static_assert(fitted.capacity() == fitted.size());
consteval auto fit_string() noexcept
Adjusts the compile-time buffer size to exactly fit the string content. C++20 or above.
Definition hybstr.hpp:901
C++17 fallback macro
constexpr auto fitted = HYBSTR_FIT_STRING(s);
static_assert(fitted.capacity() == fitted.size());
Comparison Operators
hybstr::string_impl supports all standard comparison operations:
static_assert(a < b);
static_assert(a != b);
Factory Functions
Function | Description |
string() | Create an empty string. |
string(const char(&)[N]) | From string literal. |
string(std::string_view) | From string view. |
string(_Iter start, _Iter end) | From iterator range. |
Mixed Usage
#include <iostream>
int main() {
constexpr auto prefix =
string(
"Hello, ");
std::string name;
std::cin >> name;
auto result = prefix +
string(name.begin(), name.end()) +
"!";
std::cout << result.view() << '\n';
}