小编Vin*_*tto的帖子

如何将模板参数限制为指针或随机访问迭代器?

有没有办法将模板函数的参数类型限制为仅指针或随机访问迭代器?

假设我正在开发一个仅适用于随机可访问容器的排序功能.我正在寻找一种方法来抛出编译时错误,如果用户通过非随机访问迭代器.

#include <type_traits>
#include <iterator>

template <class Iterator> void mySort(Iterator begin, Iterator end){

    /*The below condition must be true if the 'Iterator' type is a pointer 
    or if it is of Category random_access_iterator_tag. How to make such check?*/
    static_assert(some condition, "The mySort() function only accepts random access iterators or raw pointers to an array.\n");

    for (Iterator it = begin; it != end; ++it){
        /*Some kind of sorting is performed here, which 
        uses arithmetic operators + and - in the …
Run Code Online (Sandbox Code Playgroud)

c++ static-assert c++11

3
推荐指数
1
解决办法
1434
查看次数

标签 统计

c++ ×1

c++11 ×1

static-assert ×1