c++ : force std::string to be casted to string_view than to a template

I have the following class declaration :

class MyClass {
public :
    template <typename StringContainer>
    explicit MyClass(const StringContainer&)
        std::cout << "String container ctor" << std::endl;

    explicit MyClass(std::string_view)
        std::cout << "string_view ctor" << std::endl;

Templated constructor version was implemented to deal with stl containers with std::string. For the snipped below I'd like to call string_view ctor but for some reason a templated version is used.

int main() {
    std::string tmp("a");
    MyClass test(tmp);

    return 0;

What is the right way to cast std::string to be used with std::string_view ctor?

Read more here: https://stackoverflow.com/questions/66343007/c-force-stdstring-to-be-casted-to-string-view-than-to-a-template

Content Attribution

This content was originally published by Dmitry at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: