Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
hal::inplace_string<Capacity>: a string of up toCapacitycharacters, stored in the object, with everythingconstexpr. It moves the compile-time text builder out of libhal-arm-mcu (#283, where it wascompile_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.Design
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. Itsat()also throwsstd::out_of_range.static_assertmessages 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.truncated()reports it, so building a message can't itself fail to compile or throw.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()andoperator==with astd::string_view.size()anddata()are whatstatic_assertneeds.Size
No code reaches flash unless a program builds a string at run time. A
static_assertmessage 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_assertwith a built message behind the pragma. All 19 host tests pass.