c++ - Range-v3 中 Readable 使用的 CommonReference 有什么作用?

标签 c++ range-v3

我发现typename gsl::span<const gsl::byte>::const_iterator不满足Readable range-v3 中的概念。在仔细研究了这个概念之后,我发现了这个约束:

template<typename I>
            auto requires_(I&&) -> decltype(
                concepts::valid_expr(
                    // The value, reference and rvalue reference types are related
                    // through the CommonReference concept.
                    concepts::model_of<CommonReference, reference_t<I> &&, value_t<I> &>(),
                    concepts::model_of<CommonReference, reference_t<I> &&, rvalue_reference_t<I> &&>(),
                    concepts::model_of<CommonReference, rvalue_reference_t<I> &&, value_t<I> const &>(),
                    // Experimental additional tests. If nothing else, this is a good workout
                    // for the common_reference code.
                    concepts::model_of<Same, ranges::common_reference_t<reference_t<I>, value_t<I>>, value_t<I>>(),
                    concepts::model_of<Same, ranges::common_reference_t<rvalue_reference_t<I>, value_t<I>>, value_t<I>>()
                ));

ranges::common_reference_t删除const来自value_type然后它们就不一样了。

CommonReference有什么作用?约束是什么意思?为什么要Readable满足他们吗?

最佳答案

您的问题出在 GSL 上。来自 span_iterator 的来源(https://github.com/Microsoft/GSL/blob/master/gsl/span#L145-L147):

using value_type = std::conditional_t<IsConst, std::add_const_t<typename Span::element_type>, typename Span::element_type>;

所以span::const_iterator有一个const -合格value_type 。这很奇怪而且错误。它可能也不符合标准。我尚未在标准中找到该断言的明确证据,但该标准具有很强的启发性。例如,这里是 std::iterator_traits 的特化对于指向 const 的指针:

template<class T> struct iterator_traits<const T*> { using difference_type = ptrdiff_t; using value_type = T; using pointer = const T*; using reference = const T&; using iterator_category = random_access_iterator_tag; };

看到了吗? value_type不是const - 限定,即使是指向 - const 的指针.

关于c++ - Range-v3 中 Readable 使用的 CommonReference 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41014661/

相关文章:

c++ - 为了多态性而指向 vector 的指针 - C++

c++ - MFC-CArray 复制

c++11 - 为什么 std::back_inserter 迭代器在范围 3 中不是弱增量的?

c++ - 复杂范围的多个迭代器

c++ - 作为类成员的通用 shared_ptr

c++ - OpenCV 断言矩阵乘法失败

c++ - set_intersection在范围范围内

c++ - 为什么 std::ranges::view_interface 使用 CRTP

c++ - std::reverse on boost::ptr_vector 切片对象?