C++ Logo

std-discussion

Advanced search

[P2516] Removal of implicit construction of string_view from everything

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Tue, 30 Aug 2022 23:43:29 +0300
Hi,

I was wondering what happened to P2516[1], which was about removing the
conversion constructor of string_view from ranges that was added in
C++23 draft.

I'm asking because this constructor is causing problems in a code base
I'm working on. Specifically, I have a custom string type that looks
like this:

  class custom_string
  {
  public:
    iterator begin();
    iterator end();
    size_type size() const;
    bool empty() const;
    const char* data() const;
    const char* c_str() const;

    operator std::string() const;
  };

Now I need to pass this string to an API that has multiple overloads for
different string types, including these:

  void foo(std::string const&); // #1
  void foo(std::string_view const&); // #2

  custom_string s;
  foo(s);

This call is ok in C++17 and C++20 and is ambiguous in C++23 because #1
can be called by converting s to std::string via custom_string::operator
std::string() and #2 can be called by constructing std::string_view from
s via the converting constructor.

So, if there is a list of broken code by that std::string_view
constructor, count this one in too.

PS: I know that the "recommended" way to solving this would be to remove
or change the conversion operator, or change the set of foo overloads,
but this isn't as simple as that. The code must work from C++03 on up,
and it is expected to support types that are convertible to std::string
but are not ranges. So no, this advice doesn't really work in practice.

[1]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2516r0.html

Received on 2022-08-30 20:43:44