Part of Kronuz C++ Libraries, a collection of small, reusable C++ libraries. Read its story in The Bag of Holding, a Familiars post.
Compile-time strings for C++20: concatenate literals, characters, and integers into a
constant the compiler builds. Each piece is a constexpr value whose length is part of
its type, and + folds the pieces into one owning array, which you then use as a
const char*, std::string_view, or std::string. Use it for fixed strings you
want to pay nothing for at runtime: ANSI color codes, table headers, labels, hash seeds.
- No runtime cost. Concatenation, integer rendering, and comparisons run in
constexpr; the result is a constant with its length in its type. - No heap. A literal is referenced in place; a concatenation lives in a fixed array.
- Drops into normal code. Converts implicitly to
const char*andstd::string_view, explicitly tostd::string, and formats withstd::formatwhen<format>is available. - Integers too.
to_string<-42>()is"-42", built by the compiler. - One header, no dependencies.
#include "static_string.hh"
#include <cstdio>
#include <string_view>
namespace ss = static_string;
constexpr auto greeting = ss::string("Hello, ") + ss::string("world") + '!';
static_assert(greeting.size() == 13);
constexpr auto label = ss::string("count=") + ss::to_string<42>();
static_assert(std::string_view(label) == "count=42");
int main() {
std::puts(greeting); // prints Hello, world!
}examples/demo.cc also builds an ANSI color escape and hands the
same bytes to std::string and std::format.
Requires a C++20 compiler and CMake 3.14 or newer; no other dependencies. CI builds and tests it with GCC and Clang on Linux (x86_64, arm64), macOS (arm64, x86_64), and FreeBSD, plus ASan, UBSan, and TSan builds.
include(FetchContent)
FetchContent_Declare(static_string
GIT_REPOSITORY https://github.com/Kronuz/static-string.git
GIT_TAG e0a3d6e7fd3e9101bafab474aba458f3cdd37850) # or main, which tracks the tip
FetchContent_MakeAvailable(static_string)
target_link_libraries(my_app PRIVATE static_string::static_string)Then #include "static_string.hh". Without CMake, copy static_string.hh onto your
include path.
Strings whose pieces are all known at compile time: escape sequences, labels, keys and prefixes built from literals and integer constants.
Not for:
- Runtime data. The length is part of the type, so
static_string<13, …>andstatic_string<14, …>are different types. Usestd::string. - Very long concatenation chains. Each step is a template instantiation, which costs compile time.
- The Bag of Holding:
the Familiars post that covers
static-stringamong the small helpers. - docs/API.md: the two flavors, the factories, the operators, and the caveats.
- ARCHITECTURE.md: how concatenation and integer rendering unfold at compile time.
- CHANGELOG.md: notable changes.
- AGENTS.md: repository map, conventions, and traps.
- Catalog entry.
cmake -B build
cmake --build build
ctest --test-dir build
./build/static_string_demoThe tests use assert, so build without NDEBUG.
Issues and pull requests are welcome. Read AGENTS.md first, and the catalog's AGENTS.md for the family's rules. CI must pass on every platform.
Boost Software License 1.0; see LICENSE. The core is Andrzej Krzemienski's 2017 constexpr string, which entered Xapiand with his notice and the full Boost license; Dubalu LLC and Germán Méndez Bravo hold the copyright on the extensions.