Search code examples
c++constructorc++17string-literalsstring-view

Pass string literal to the function argument which constructor only takes std::string_view


Suppose I have an object only has std::string_view constructor:

struct OnlyStringViewCtor {
  std::string str_;
  OnlyStringViewCtor(std::string_view str) : str_(str) {}
};

and there is a function that takes const OnlyStringViewCtor& as parameter:

void f(const OnlyStringViewCtor&) {}

when I call f("hello") directly there is a compiler error:

error: invalid initialization of reference of type 'const OnlyStringViewCtor&' from expression of type 'const char [6]'

Is there some nice way that can let f("hello") work fine and not declare another constructor such as OnlyStringViewCtor(const char*)?


Solution

  • As the other answer explained, the compiler won't do multiple implicit conversions.

    But, you can use a template to bridge the gap:

    struct OnlyStringViewCtor {
      std::string str_;
      template<typename T, std::enable_if_t<std::is_constructible_v<std::string, T>, int> = 0>
      OnlyStringViewCtor(T str) : str_(str) {}
    };
    

    https://godbolt.org/z/1reajv