Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
text_static_string.h
1#ifndef SOURCEMETA_CORE_TEXT_STATIC_STRING_H_
2#define SOURCEMETA_CORE_TEXT_STATIC_STRING_H_
3
4#include <cstddef> // std::size_t
5
6namespace sourcemeta::core {
7
21public:
29 template <std::size_t Length>
30 // Binding to the array rather than to a pointer is what rejects a message
31 // that does not outlive the error, and the implicit conversion is what lets
32 // a throw site read as a bare literal. It is not noexcept, as the throw
33 // below is what rejects a bad argument, and nothing observes that, since a
34 // consteval constructor never runs
35 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions,modernize-avoid-c-arrays)
36 consteval StaticString(const char (&value)[Length]) : value_{value} {
37 // A throw-expression is not a constant expression, which is how an array
38 // that is not null-terminated becomes a compile-time error rather than a
39 // read past its end. It can never run, as the only way to reach it is an
40 // argument that does not compile
41 if (value[Length - 1] != '\0') {
42 throw "A static string must be null-terminated";
43 }
44 }
45
56 [[nodiscard]] constexpr auto data() const noexcept -> const char * {
57 return this->value_;
58 }
59
60private:
61 const char *value_;
62};
63
64} // namespace sourcemeta::core
65
66#endif
consteval StaticString(const char(&value)[Length])
Definition text_static_string.h:36
constexpr auto data() const noexcept -> const char *
Definition text_static_string.h:56