Quantcast
Channel: Why doesn't std::string have a constructor that directly takes std::string_view? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Why doesn't std::string have a constructor that directly takes std::string_view?

$
0
0

To allow std::string construction from std::string_viewthere is a template constructor

template<class T>explicit basic_string(const T& t, const Allocator& alloc = Allocator());

which is enabled only if const T& is convertible to std::basic_string_view<CharT, Traits> (link).

In the meantime there is a special deduction guide to deduce basic_string from basic_string_view (link). A comment to the guide says:

Guides (2-3) are needed because the std::basic_string constructors for std::basic_string_views are made templates to avoid causing ambiguities in existing code, and those templates do not support class template argument deduction.

So I'm curious, what is that ambiguity that requires to have that deduction guide and template constructor instead of simply a constructor that takes std::basic_string_view, e.g. something like

explicit basic_string(basic_string_view<CharT, Traits> sv, const Allocator& alloc = Allocator());

Note that I'm not asking why the constructor is marked explicit.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images