-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
constexpr StringView without literals #123
Comments
There's also The |
The main reason is OTOH, as @pezcode says, it's possible to get away with using the char array length for size, although it may give different results than It'd then be something like the following, it's very explicit but since an arbitrary // will be constexpr once I make that possible
constexpr Containers::StringView hello{Containers::arrayView("hello world!!").except(1), // strips the \0
// you're responsible for ensuring these are correct
Containers::StringViewFlag::Global|Containers::StringViewFlag::NullTerminated}; About the
|
How does constexpr std::string_view a = "foo\0bar";
std::string_view b = "foo\0bar";
std::cout << a.size() << " " << b.size() << std::endl; outputs Thanks for the clarification on the |
Hmm. Using std::char_traits::length(), which needs a good amount of compiler magic, apparently. This is the implementation in libstdc++ 11: static _GLIBCXX17_CONSTEXPR size_t
length(const char_type* __s)
{
#if __cplusplus >= 201703L
if (__constant_string_p(__s))
return __gnu_cxx::char_traits<char_type>::length(__s); // <-- and this is the dump loop, yes
#endif
return __builtin_strlen(__s);
} (Sigh.) For obvious reasons I don't want to depend on the |
Why do you need consteval? We could reimplement a constrexpr constexpr std::size_t length(const char* start) {
const char* end = start;
for(; *end != '\0'; ++end)
;
return end - start;
}
constexpr size_t s = length("foo\0bar"); |
Because, as I said above, I don't want to give up on a possibility to use 128/256-bit SSE4.2 / AVX instructions at runtime instead of always iterating on a single byte at a time and then hoping the compiler optimizes that somehow. That's what |
Got it. Would be nice if we could dispatch code based on whether the arg is constexpr or not... |
Yeah that's what |
A constexpr Containers::StringView a = Containers::arrayView("hello").exceptSuffix(1); Everything else, related to |
Hi. I noticed that a
constexpr Containers::StringView
can only be created fromconst char *
via a literal_s
. Reading the doc this seems to be a deliberate design decision, but I'm not entirely sure why? E.g., the tagGlobal
is documented as:But why would you want to extend the lifetime of a passed string view? This is a view, not a smart pointer no?
The text was updated successfully, but these errors were encountered: