Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/iter t"

From cppreference.com
< cpp‎ | iterator
m (langlinks)
(Link update.)
 
(17 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{cpp/title|iter_value_t|iter_reference_t|iter_difference_t|iter_rvalue_reference_t|iter_common_reference_t}}
+
{{cpp/title|iter_value_t|iter_reference_t|iter_const_reference_t|iter_difference_t|iter_rvalue_reference_t|iter_common_reference_t}}
 
{{cpp/iterator/navbar}}
 
{{cpp/iterator/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | iterator}}
+
{{dcl header|iterator}}
{{dcl |1=
+
{{dcl|num=1|since=c++20|1=
template< class T >
+
concept /*dereferenceable*/ = /* see below */; // exposition only
+
}}
+
{{dcl | num=1 | since=c++20 | 1=
+
 
template< class T >
 
template< class T >
 
using iter_value_t = /* see below */;
 
using iter_value_t = /* see below */;
 
}}
 
}}
{{dcl | num=2 | since=c++20 | 1=
+
{{dcl|num=2|since=c++20|1=
 
template< /*dereferenceable*/ T >
 
template< /*dereferenceable*/ T >
 
using iter_reference_t = decltype(*std::declval<T&>());
 
using iter_reference_t = decltype(*std::declval<T&>());
 
}}
 
}}
{{dcl | num=3 | since=c++20 | 1=
+
{{dcl|num=3|since=c++23|1=
 +
template< std::indirectly_readable T >
 +
using iter_const_reference_t =
 +
    std::common_reference_t<const std::iter_value_t<T>&&,
 +
                            std::iter_reference_t<T>>;
 +
}}
 +
{{dcl|num=4|since=c++20|1=
 
template< class T >
 
template< class T >
 
using iter_difference_t = /* see below */;
 
using iter_difference_t = /* see below */;
 
}}
 
}}
{{dcl | num=4 | since=c++20 | 1=
+
{{dcl|num=5|since=c++20|1=
 
template< /*dereferenceable*/ T>
 
template< /*dereferenceable*/ T>
 
     requires /* see below */
 
     requires /* see below */
using iter_rvalue_reference_t = decltype(ranges::iter_move(std::declval<T&>()));
+
using iter_rvalue_reference_t =
 +
    decltype(ranges::iter_move(std::declval<T&>()));
 
}}
 
}}
{{dcl | num=5 | since=c++20 | 1=
+
{{dcl|num=6|since=c++20|1=
template< Readable T >
+
template< std::indirectly_readable T >
using iter_common_reference_t = std::common_reference_t<std::iter_reference_t<T>,
+
using iter_common_reference_t =
                                                        std::iter_value_t<T>&>;
+
    std::common_reference_t<std::iter_reference_t<T>,
 +
                            /*indirect-value-t*/<T>>;
 +
}}
 +
{{dcl h|Helper templates}}
 +
{{dcla|num=7|expos=yes|anchor=dereferenceable|1=
 +
template< class T >
 +
concept /*dereferenceable*/ = /* see below */;
 +
}}
 +
{{dcla|num=8|expos=yes|anchor=indirect-value-t|1=
 +
template< std::indirectly_readable T >
 +
using /*indirect-value-t*/ = /* see below */;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Compute the associated types of an iterator. The exposition-only concept {{tt|''dereferenceable''}} is satisfied if and only if the expression {{tt|*std::declval<T&>()}} is valid and has a referenceable type (in particular, not {{c|void}}).
+
Compute the associated types of an iterator.
 +
 
 +
@1@ Computes the {{rlp|/#Types and writability|value type}} of {{tt|T}}.
 +
* If {{c/core|std::iterator_traits<std::remove_cvref_t<T>>}} is not specialized, then {{c/core|std::iter_value_t<T>}} is {{c/core|std::indirectly_readable_traits<std::remove_cvref_t<T>>::value_type}}.
 +
* Otherwise, it is {{c/core|std::iterator_traits<std::remove_cvref_t<T>>::value_type}}.
  
@1@ Computes the ''value type'' of {{tt|T}}. If {{c|std::iterator_traits<T>}} is not specialized, then {{c|std::iter_value_t<T>}} is {{c|std::readable_traits<T>::value_type}}. Otherwise, it is {{c|std::iterator_traits<T>::value_type}}.
 
 
@2@ Computes the ''reference type'' of {{tt|T}}.
 
@2@ Computes the ''reference type'' of {{tt|T}}.
@3@ Computes the ''difference type'' of {{tt|T}}. If {{c|std::iterator_traits<T>}} is not specialized, then {{c|std::iter_difference_t<T>}} is {{c|std::incrementable_traits<T>::difference_type}}. Otherwise, it is {{c|std::iterator_traits<T>::difference_type}}.
+
 
@4@ Computes the ''rvalue reference type'' of {{tt|T}}. The "see below" portion of the constraint on this alias template is satisfied if and only if the expression {{c|ranges::iter_move(std::declval<T&>())}} is valid and has a referenceable type (in particular, not {{c|void}}).
+
@3@ Computes the ''const reference type'' of {{tt|T}}.
@5@ Computes the ''common reference type'' of {{tt|T}}. This is the common reference type between its reference type and an lvalue reference to its value type.
+
 
 +
@4@ Computes the {{rlp|/#Types and writability|difference type}} of {{tt|T}}.
 +
* If {{c/core|std::iterator_traits<std::remove_cvref_t<T>>}} is not specialized, then {{c/core|std::iter_difference_t<T>}} is {{c/core|std::incrementable_traits<std::remove_cvref_t<T>>::difference_type}}.
 +
* Otherwise, it is {{c/core|std::iterator_traits<std::remove_cvref_t<T>>::difference_type}}.
 +
 
 +
@5@ Computes the ''rvalue reference type'' of {{tt|T}}. The constraint on this alias template is satisfied if and only if the expression {{c|ranges::iter_move(std::declval<T&>())}} is valid and has a referenceable type.
 +
 
 +
@6@ Computes the ''common reference type'' of {{tt|T}}. This is the common reference type between its reference type and an lvalue reference to its value type.
 +
 
 +
@7@ The exposition-only concept {{tti|dereferenceable}} is satisfied if and only if the expression {{c|*std::declval<T&>()}} is valid and has a [[cpp/meta#Definitions|referenceable type]].
 +
 
 +
@8@ The exposition-only alias template {{tti|indirect-value-t}} denotes the following:
 +
* {{c/core|std::invoke_result_t<Proj&, /*indirect-value-t*/<I>>}} if {{tt|T}} is the same as {{c/core|std::projected<I, Proj>}} for some types {{tt|I}} and {{tt|Proj}}.
 +
* Otherwise, {{c/core|std::iter_value_t<T>&}}.
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|paper=P2609R3|std=C++20|before={{tt|std::iter_common_reference_t}} was defined in terms<br>of {{c/core|std::iter_value_t<T>&}} which incorrectly handled<br>{{lc|std::projected}} types that project into rvalue reference types|after=defined in terms of<br>{{c/core|/*indirect-value-t*/<T>}}<br>to handle such cases}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/iterator/dsc Readable}}
+
{{dsc inc|cpp/iterator/dsc indirectly_readable}}
{{dsc inc | cpp/iterator/dsc WeaklyIncrementable}}
+
{{dsc inc|cpp/iterator/dsc weakly_incrementable}}
{{dsc inc | cpp/iterator/dsc readable_traits}}
+
{{dsc inc|cpp/iterator/dsc indirectly_readable_traits}}
{{dsc inc | cpp/iterator/dsc incrementable_traits}}
+
{{dsc inc|cpp/iterator/dsc incrementable_traits}}
{{dsc inc | cpp/iterator/dsc iterator_traits}}
+
{{dsc inc|cpp/iterator/dsc iterator_traits}}
 +
{{dsc inc|cpp/ranges/dsc iterator_t}}
 +
{{dsc inc|cpp/ranges/dsc range_reference_t}}
 +
{{dsc inc|cpp/ranges/dsc range_size_t}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 22:50, 2 September 2024

 
 
Iterator library
Iterator concepts
Iterator primitives
iter_value_titer_difference_titer_reference_titer_const_reference_titer_rvalue_reference_titer_common_reference_t
(C++20)(C++20)(C++20)(C++23)(C++20)(C++20)
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
Defined in header <iterator>
template< class T >
using iter_value_t = /* see below */;
(1) (since C++20)
template< /*dereferenceable*/ T >
using iter_reference_t = decltype(*std::declval<T&>());
(2) (since C++20)
template< std::indirectly_readable T >

using iter_const_reference_t =
    std::common_reference_t<const std::iter_value_t<T>&&,

                            std::iter_reference_t<T>>;
(3) (since C++23)
template< class T >
using iter_difference_t = /* see below */;
(4) (since C++20)
template< /*dereferenceable*/ T>

    requires /* see below */
using iter_rvalue_reference_t =

    decltype(ranges::iter_move(std::declval<T&>()));
(5) (since C++20)
template< std::indirectly_readable T >

using iter_common_reference_t =
    std::common_reference_t<std::iter_reference_t<T>,

                            /*indirect-value-t*/<T>>;
(6) (since C++20)
Helper templates
template< class T >
concept /*dereferenceable*/ = /* see below */;
(7) (exposition only*)
template< std::indirectly_readable T >
using /*indirect-value-t*/ = /* see below */;
(8) (exposition only*)

Compute the associated types of an iterator.

1) Computes the value type of T.
2) Computes the reference type of T.
3) Computes the const reference type of T.
4) Computes the difference type of T.
5) Computes the rvalue reference type of T. The constraint on this alias template is satisfied if and only if the expression ranges::iter_move(std::declval<T&>()) is valid and has a referenceable type.
6) Computes the common reference type of T. This is the common reference type between its reference type and an lvalue reference to its value type.
7) The exposition-only concept dereferenceable is satisfied if and only if the expression *std::declval<T&>() is valid and has a referenceable type.
8) The exposition-only alias template indirect-value-t denotes the following:
  • std::invoke_result_t<Proj&, /*indirect-value-t*/<I>> if T is the same as std::projected<I, Proj> for some types I and Proj.
  • Otherwise, std::iter_value_t<T>&.

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
P2609R3 C++20 std::iter_common_reference_t was defined in terms
of std::iter_value_t<T>& which incorrectly handled
std::projected types that project into rvalue reference types
defined in terms of
/*indirect-value-t*/<T>
to handle such cases

[edit] See also

specifies that a type is indirectly readable by applying operator *
(concept) [edit]
specifies that a semiregular type can be incremented with pre- and post-increment operators
(concept) [edit]
computes the value type of an indirectly_readable type
(class template) [edit]
computes the difference type of a weakly_incrementable type
(class template) [edit]
provides uniform interface to the properties of an iterator
(class template) [edit]
obtains iterator and sentinel types of a range
(alias template)[edit]
obtains reference types of a range
(alias template)[edit]
obtains size, difference, and value types of a range
(alias template)[edit]