Skip to content

✨ (minor) v6: Add hal::inplace_string for compile-time text - #142

Closed
kammce wants to merge 1 commit into
mainfrom
v6-inplace-string
Closed

kammce wants to merge 1 commit into
mainfrom
v6-inplace-string

Conversation

@kammce

@kammce kammce commented Nov 28, 2026

Copy link
Copy Markdown
Member

Adds hal::inplace_string<Capacity>: a string of up to Capacity characters, stored in the object, with everything constexpr. It moves the compile-time text builder out of libhal-arm-mcu (#283, where it was compile_time_text) so any library can use it. This may not merge if we find a better solution; it's here so arm-mcu doesn't keep its own.

consteval hal::inplace_string<64> pins_message()
{
  hal::inplace_string<64> text("TIM3 has these pins:");
  for (auto const pin : { 6, 7 }) {
    text.append(" PA").append_number(pin);
  }
  return text;
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wc++26-extensions"
static_assert(pins_are_valid, pins_message());
#pragma GCC diagnostic pop

Design

  • Its length changes within a fixed capacity, like C++26's std::inplace_vector, which it is named after, and it never allocates. That is what text built in a loop or from a table needs: its length isn't known until it is built. A string whose length is part of its type, such as mp-units' fixed_string, can only be built by concatenating strings of known lengths. Its at() also throws std::out_of_range.
  • Main use: static_assert messages that say what would have worked. A built message is a C++26 feature (P2741). GCC 14 and Clang 20 accept it in C++23 behind -Wc++26-extensions, which a pragma turns off; Clang reads GCC's pragma. The message is only built if the assertion fails.
  • Appending never fails. Text past the capacity is dropped, and truncated() reports it, so building a message can't itself fail to compile or throw.
  • API: push_back(), append(std::string_view), append_number() for any integer in decimal (including the most negative 64-bit value), size(), capacity(), empty(), data() (null-terminated), view() and operator== with a std::string_view. size() and data() are what static_assert needs.

Size

No code reaches flash unless a program builds a string at run time. A static_assert message is built only by the compiler.

Tested

Host tests with clang-tidy: empty, appending text and characters, numbers including both 64-bit limits, truncation past the capacity, filling exactly to the capacity, building at compile time, and a real static_assert with a built message behind the pragma. All 19 host tests pass.

A string of up to Capacity characters stored in the object, all constexpr,
so text whose length isn't known until it's built, such as a static_assert
message listing valid choices from a table, can be built at compile time.
@kammce kammce closed this Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant