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*)?
As the other answer explained, the compiler won't do multiple implicit conversions.
But, you can use a template to bridge the gap:
https://godbolt.org/z/1reajv