string_view constructor isn't constexpr
Closed this issue · 6 comments
boost::string_view
could be a good choice for compilers that do not support constexpr
string_view
yet. But with boost::string_view
one cannot create constexpr globals either because the converting constructor isn't constexpr. It calls char_traits::length
, and the latter is not declared constexpr. Could the definition of the constructor be fixed to use a custom implementation of length
, at least for type char
?
See the example of what I describe: https://wandbox.org/permlink/LwJnxVrsHyY1V156
You can use your own char_traits
and declare the length method constexpr.
Looking at the declaration of operator<<
:
template<class charT, class traits>
inline std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os,
const basic_string_view<charT,traits>& str);
I would also have to provide custom ostream
specializations, and could not use my string_ref
with std::cout
.
I would really rather not do this, because the user expects traits::length
to be called.
You could write (untested code):
constexpr string_view make_string_view(const char *s)
{ return string_view(s, my_length(s)); }
and call that instead of using the constructor directly.
The user can't expect std::char_traits<char>::length
to be called because it's unobservable.
besides std::string_view ctors are constexpr and it'seems len traits also constexpr
cos i can write like this
constexpr std::string_view sv = "Hello";
without any compiler errors
but in this expression
constexpr boost::string_view sv = "Hello";
i've got compiler errors
boost::string_view
constructor is constexpr now, and as long as std::char_traits::length()
is constexpr, it works fine.