c++ iterator of any container with specific value type using concepts
To fit in with the C++20 Ranges ecosystem:
template <std::input_iterator I, std::sentinel_for<I> S>
requires std::same_as<std::iter_value_t<I>, MyClass>
constexpr void myFunction(I begin, S end)
{
// ...
}
Probably not the easiest to understand reference, but the normative source of information for concepts is the available standard draft. Where a concept definition is specified grammatically as
1 A concept is a template that defines constraints on its template arguments.
concept-definition: concept concept-name = constraint-expression ; concept-name: identifier
It's pretty much just like a bool variable template constant, but it's defined with the concept keyword. So to translate your condition directly to a concept is essentially this
template<typename T>
concept MyClassIter = std::is_same_v<
MyClass,
typename std::iterator_traits<T>::value_type
>;
With the concept in hand, we can use it as a type constraint on a template's type parameter, thus transforming your template into this
template <MyClassIter IteratorType>
void myFunction( IteratorType begin, IteratorType end ) {}
If the constraint is not satisfied for a type, this overload is discarded. Not satisfied in this context also includes substitution failures. So it's the same condition you had originally.
Live example