Reputation: 4305
I have the following source code:
std::string s;
std::string_view sv(s.begin(), s.end());
that compiles with MSVC and GCC, but does not compile with Apple Clang.
What is the right way to write a function like
template< class It >
constexpr std::string_view make_string_view( It first, It last );
that would compile with both C++17 and C++20 and use new std::string_view
constructor accepting two iterators if possible?
Upvotes: 2
Views: 105
Reputation: 520
You would need to check at compile time that:
std::string_view::value_type
You may check by using the std::iterator_traits
class, std::is_same_v
type trait and static_assert
.
Having that done you could calculate the distance from first to last (by using std::distance
) and call the following std::string_view
constructor:
constexpr basic_string_view( const CharT* s, size_type count );
For s
, you could pass std::addressof(*first)
, and for the count the result of the std::distance
call.
Upvotes: 5