检查/修改迭代器“constness”

时间:2013-05-11 15:23:17

标签: c++ templates iterator const

我有两个半密切相关的问题。给定一个STL迭代器类型作为模板参数传递:

  1. 如何确定类型是对应于const或非const迭代器?
  2. 除了1.,如何强制(例如使用enable_if)此类型对应于非const迭代器?
  3. 如何从非常数中获取迭代器的常量(而反之亦然)? [注:在this post中回答;不足为奇,你做不到。 ]
  4. 这个问题来自哪里:

    我写了一个小类来促进对向量的算术/关系/代数运算(通过向量我的意思是1d固定大小的数据,而不是STL向量)。我没有强制使用特定的数据容器,而是定义了一个接口并派生了几个可能的容器,它们基本上“包装”了各种存储数据的方式。其中一个容器是STL迭代器的包装器,我遇到了一些麻烦。

3 个答案:

答案 0 :(得分:6)

问题1:

您可以使用以下类型特征:

template<typename T, typename = void>
struct is_const_iterator : std::false_type { };

template<typename T>
struct is_const_iterator<T,
    typename std::enable_if<
        std::is_const<
            typename std::remove_pointer<
                typename std::iterator_traits<T>::pointer
                >::type
            >::value
        >::type> : std::true_type { };

这是一个演示:

#include <type_traits>
#include <iterator>
#include <list>
#include <vector>

template<typename T, typename = void>
struct is_const_iterator : std::false_type { };

template<typename T>
struct is_const_iterator<T,
    typename std::enable_if<
        std::is_const<
            typename std::remove_pointer<
                typename std::iterator_traits<T>::pointer
                >::type
            >::value
        >::type> : std::true_type { };

int main()
{
    typedef std::list<int>::iterator LI;
    typedef std::list<int>::const_iterator CLI;
    static_assert(is_const_iterator<LI>::value, "!"); // Fires
    static_assert(is_const_iterator<CLI>::value, "!"); // Does not fire

    typedef std::vector<int>::iterator VI;
    typedef std::vector<int>::const_iterator CVI;
    static_assert(is_const_iterator<VI>::value, "!"); // Fires
    static_assert(is_const_iterator<CVI>::value, "!"); // Does not fire
}

这是一个live example

问题2:

使用上述类型特征,这变得简单。假设您有一个要约束的函数模板foo(),以便它只接受非const迭代器:

template<typename It,
    typename std::enable_if<!is_const_iterator<It>::value>::type* = nullptr>
void foo(It i)
{
    // Does something with i...
}

一个简单的演示程序:

int main()
{
    std::vector<int> v;
    foo(v.begin()); // OK
    foo(v.cbegin()); // ERROR!
}

这是一个live example

答案 1 :(得分:3)

对于1),你可以这样做:

std::is_const<
  typename std::remove_reference<
    typename std::iterator_traits<Iterator>::reference
  >::type
>::value

或者这个:

std::is_const<
  typename std::remove_reference<
    decltype(*iterator)
  >::type
>::value

您可以使用这些谓词传递给std::enable_if来实现2)。

注意:正如R. Martinho Fernandes在评论中指出的那样,如果所讨论的迭代器使用与其reference特征的普通引用不同的类型(例如正如std::vector<bool>::const_iterator所做的那样。)

答案 2 :(得分:2)

您可以在

上使用SFINAE
decltype( **(T*)0 = std::move(**(T*)0) )

或(Xeo的偏好)

decltype( *declval<T&>() = std::move(*declval<T&>()) )

检查解除引用迭代器是否为您提供了可分配的东西。如果集合的元素类型不可分配,那么不完美,但是不管怎么说非const_iterator会有什么好处?

不要测试const_iterator,测试算法实际需要的操作。