Search code examples
c++pointersiteratorstatic-assertenable-if

Asserting a Template Argument is an Iterator/Pointer


I have a templatized function that takes in pointers.

template <typename T>
void foo(const T* bar){}

How can I change foo to ensure that I am being passed an iterator/pointer? I assume there is a static_assert or an enable_if way of doing this, but I can't seem to find it.


Solution

  • You may use std::iterator_traits to check if it is an iterator (or pointer)

    template <typename IT>
    decltype(std::iterator_traits<IT>::iterator_category{}, void())
    foo(IT bar);