I have the function
// Helper to determine whether there's a const_iterator for T.
template <typename T>
struct hasConstIt {
private:
template<typename C> static char test(typename C::const_iterator*);
template<typename C> static int test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
// Check if a container contains an element.
template <typename Container, typename = std::enable_if_t<hasConstIt<Container>::value> >
bool contains(const Container & container, typename Container::value_type const & element) {
return std::find(container.begin(), container.end(), element) != container.end();
}
How can I specify that the container value type must be a specific type? Say I want the container to contain int
, then valid containers could be vector<int>
, deque<int>
etc.