I've a function to check if a std::string
contains a substring. I pass the strings around as std::string_view
, such that no copy takes place.
bool containsSubstr(std::string_view str, std::string_view substr)
{
return str.find(substr) != std::string::npos;
}
I now want to make a function, using the new C++17 fold expressions, to check if a string contains several substrings. Again, I want to pass them by std::string_view
s.
How can I do that?
template<typename... Substrs>
bool containsAllSubstr(std::string_view str, Substrs... substrs)
{
return (containsSubstr(str, substrs) && ...);
}
As far as I know, the above version will take the substring's as the type with which they came in. So a std::string
would be copied. How can I fix the type to std::string_view
? Something like:
template<> // does not compile
bool containsAllSubstr(std::string_view str, std::string_view... substrs)
{
return (containsSubstr(str, substrs) && ...);
}