STL container with a specific type as a generic argument
You can make foo
a function template taking a template template parameter for the container type.
e.g.
template<template<typename...> typename C>
void foo(const C<std::string> &cont)
{
for(std::string val: cont) {
std::cout << val << std::endl;
}
}
LIVE
Depending on if you want to overload foo
for other cases or not
// Doesn't participate in overload resolution when not applicable
template<typename Container, typename = std::enable_if_t<std::is_same_v<typename Container::value_type, std::string>>>
void foo(const Container &cont) {
for(std::string val: cont) {
std::cout << val << std::endl;
}
}
// simpler
template<typename Container>
void foo(const Container &cont) {
static_assert(std::is_same_v<typename Container::value_type, std::string>, "Container must contain std::string")
for(std::string val: cont) {
std::cout << val << std::endl;
}
}
You might use a different test to std::is_same
, such as std::is_convertible
to allow
std::vector<char *> c_strings;
foo(c_strings);