How to restrict template parameter to pointer or random access iterator only?

后端 未结 1 1453
轻奢々
轻奢々 2021-01-19 07:19

Is there a way to restrict the parameter type of a template function to only pointers or random-access iterators?

Say I am developing a sorting function which works

相关标签:
1条回答
  • 2021-01-19 07:57

    Use iterator traits:

    static_assert(
        std::is_same<std::random_access_iterator_tag,
                     typename std::iterator_traits<Iterator>::iterator_category>::value,
        "The mySort() function only accepts random access iterators or raw pointers to an array.\n");
    
    0 讨论(0)
提交回复
热议问题