I have a template function that I want to enable only for standard containers (or containers compatible with standard containers, which at least provide a begin()
member function). I'm SFINAE-ing out non-containers in the following way:
template<typename Container>
typename Container::value_type
f(const Container& c,
typename std::enable_if<
std::is_same<
decltype(*c.begin()),
typename Container::value_type
>::value
>::type* = nullptr)
{
// implementation here
}
The std::is_same
and decltype
don't look too elegant. Is there any better way of doing this?
PS: I need the SFINAE here because I have a different overload
template<typename Derived>
f(const Eigen::MatrixBase<Derived>& A)
and whenever I try f(some_Eigen_matrix)
, the Container
overload ends up being picked up, then the compiler spits out an error because the type is lacking begin()
.