You can use std::is_same
to check if a type is exaclty the same as a specified one. If you want to check both of your conditions at once, you can simply use logical operators in the std::enable_if
conditon:
template <typename Container, typename = std::enable_if_t<
(std::is_same<typename Container::value_type, int>::value &&
hasConstIt<Container>::value)
> >
bool contains(const Container & container, typename Container::value_type const & element) {
return std::find(container.begin(), container.end(), element) != container.end();
}
Contrary to the other answer, this is SINAE friendly. But if you only want to make sure it does not compile if the user makes a mistake I'd go with the other solution because it will give you way better error messages.
Example here.
If you can use C++20 you can use requires
to constrain your template types. This is both SFINAE-friendly and gives you a nice error message.
template<typename Container>
requires (
requires {
typename Container::const_iterator;
typename Container::value_type;
}
&& std::same_as<typename Container::value_type, int>
)
bool contains (const Container & container, typename Container::value_type const & element) {
return true;
}
Or, if you need to constrain multiple functions you can define a concept
template<typename Container>
concept good_container =
requires(Container c) {
typename Container::const_iterator;
typename Container::value_type;
}
&& std::same_as<typename Container::value_type, int>;
template<good_container Container>
bool contains (const Container & container, typename Container::value_type const & element) {
return true;
}
Example here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…