How to use a fold expression with a specific type?

I have a function to check if a std::stringsubstring contains . I pass the strings as std::string_view, so the copy is not executed.

bool containsSubstr(std::string_view str, std::string_view substr)
{
    return str.find(substr) != std::string::npos;
}

Now I want to make a function using the new C ++ 17 fold expressions to check if a string contains multiple substrings. Again, I want to pass them std::string_views.

How can i do this?

template<typename... Substrs>
bool containsAllSubstr(std::string_view str, Substrs... substrs)
{
    return  (containsSubstr(str, substrs) && ...);
}

As far as I know, in the above version the substring will be used as the type with which they entered. Thus it will be copied std::string. How can I fix the type before std::string_view? Sort of:

template<> // does not compile
bool containsAllSubstr(std::string_view str, std::string_view... substrs)
{
    return  (containsSubstr(str, substrs) && ...);
}
+6
source share
1 answer

. ( const&):

template <typename... Substrs>
bool containsAllSubstr(std::string_view str, Substrs const&... substrs)
{
    return  (containsSubstr(str, substrs) && ...);
}

, -, string_view. SFINAE-, :

template <typename... Substrs,
    std::enable_if_t<(std::is_convertible_v<Substrs const&, std::string_view> && ...), int> = 0>
bool containsAllSubstr(std::string_view str, Substrs const&... substrs)
{
    return  (containsSubstr(str, substrs) && ...);
}

, , :

template <size_t N>
bool containsAllSubstr(std::string_view str, std::string_view (&substrs)[N]);

. . :

bool containsAllSubstr(std::string_view str, std::initializer_list<std::string_view> substrs);
+9