Static assert that LiteralStringRef arg is literal string

This commit is contained in:
Andrew Noyes 2021-07-19 14:10:37 -07:00
parent f0bc23de8b
commit 459e35f261
1 changed files with 9 additions and 1 deletions

View File

@ -662,7 +662,15 @@ struct Traceable<Standalone<T>> : std::conditional<Traceable<T>::value, std::tru
static std::string toString(const Standalone<T>& value) { return Traceable<T>::toString(value); }
};
#define LiteralStringRef(str) StringRef((const uint8_t*)(str), sizeof((str)) - 1)
namespace literal_string_ref {
template <class T, int Size>
StringRef LiteralStringRefHelper(const char* str) {
static_assert(std::is_same_v<T, const char(&)[Size]>, "Argument to LiteralStringRef must be a literal string");
return StringRef(reinterpret_cast<const uint8_t*>(str), Size);
}
} // namespace literal_string_ref
#define LiteralStringRef(str) literal_string_ref::LiteralStringRefHelper<decltype(str), sizeof(str)>(str)
inline StringRef operator"" _sr(const char* str, size_t size) {
return StringRef(reinterpret_cast<const uint8_t*>(str), size);
}