Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/incrementable traits"

From cppreference.com
< cpp‎ | iterator
m (P1754R1)
 
(4 intermediate revisions by 4 users not shown)
Line 2: Line 2:
 
{{cpp/iterator/navbar}}
 
{{cpp/iterator/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | iterator}}
+
{{dcl header|iterator}}
{{dcl | num=1 | since=c++20 |
+
{{dcl|num=1|since=c++20|
 
template< class I >
 
template< class I >
struct incrementable_traits { };
+
struct incrementable_traits {};
 
}}
 
}}
{{dcl | num=2 | since=c++20 |
+
{{dcl|num=2|since=c++20|
 
template< class T >
 
template< class T >
 
     requires std::is_object_v<T>
 
     requires std::is_object_v<T>
 
struct incrementable_traits<T*>;
 
struct incrementable_traits<T*>;
 
}}
 
}}
{{dcl | num=3 | since=c++20 |
+
{{dcl|num=3|since=c++20|
 
template< class T >
 
template< class T >
struct incrementable_traits<const T> : incrementable_traits<T> { };
+
struct incrementable_traits<const T>
 +
    : incrementable_traits<T> {};
 
}}
 
}}
{{dcl | num=4 | since=c++20 |
+
{{dcl|num=4|since=c++20|
 
template< class T >
 
template< class T >
  requires requires { typename T::difference_type; }
+
    requires requires { typename T::difference_type; }
 
struct incrementable_traits<T>;
 
struct incrementable_traits<T>;
 
}}
 
}}
{{dcl | num=5 | since=c++20 |
+
{{dcl|num=5|since=c++20|
 
template< class T >
 
template< class T >
  requires (!requires { typename T::difference_type; }) &&
+
    requires (!requires { typename T::difference_type; }) &&
          requires(const T& a, const T& b) { { a - b } -> std::integral; }
+
            requires(const T& a, const T& b) { { a - b } -> std::integral; }
 
struct incrementable_traits<T>;
 
struct incrementable_traits<T>;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Computes the associated difference type of the type {{tt|I}}, if any. Users may specialize {{tt|incrementable_traits}} for a program-defined type.
+
Computes the associated difference type of the type {{tt|I}}, if any. A program may specialize {{tt|incrementable_traits}} for a {{lsd|cpp/language/type#Program-defined type}}.
  
 
@1@ Primary template is an empty struct.
 
@1@ Primary template is an empty struct.
@2@ Specialization for pointers. Provides a member type {{tt|difference_type}} equal to {{lc|std::ptrdiff_t}}.
+
 
 +
@2@ Specialization for pointers.
 +
@@ Provides a member type {{tt|difference_type}} same as {{lc|std::ptrdiff_t}}.
 +
 
 
@3@ Specialization for const-qualified types.
 
@3@ Specialization for const-qualified types.
@4@ Specialization for types that define a public and accessible member type {{tt|difference_type}}. Provides a member type {{tt|difference_type}} equal to {{tt|T::difference_type}}.  
+
 
@5@ Specialization for types that do not define a public and accessible member type {{tt|difference_type}} but do support subtraction. Provides a member type {{tt|difference_type}} equal to {{c|std::make_signed_t<decltype(std::declval<T>() - std::declval<T>())>}}. The implicit expression variations rule (see below) applies to the expression {{tt|a - b}}.
+
@4@ Specialization for types that define a public and accessible member type {{tt|difference_type}}.
 +
@@ Provides a member type {{tt|difference_type}} same as {{tt|T::difference_type}}.
 +
 
 +
@5@ Specialization for types that do not define a public and accessible member type {{tt|difference_type}} but do support subtraction.
 +
@@ Provides a member type {{tt|difference_type}} same as {{c/core|std::make_signed_t<decltype(std::declval<T>() - std::declval<T>())>}}. The implicit expression variations rule (see below) applies to the expression {{c|a - b}}.
  
 
{{cpp/concepts/implicit expression variations}}
 
{{cpp/concepts/implicit expression variations}}
Line 44: Line 52:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/iterator/dsc WeaklyIncrementable}}
+
{{dsc inc|cpp/iterator/dsc weakly_incrementable}}
{{dsc inc | cpp/iterator/dsc iter_t}}
+
{{dsc inc|cpp/iterator/dsc iter_t}}
{{dsc inc | cpp/iterator/dsc iterator_traits}}
+
{{dsc inc|cpp/iterator/dsc iterator_traits}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 17:01, 13 March 2024

 
 
Iterator library
Iterator concepts
Iterator primitives
(deprecated in C++17)
incrementable_traits
(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 I >
struct incrementable_traits {};
(1) (since C++20)
template< class T >

    requires std::is_object_v<T>

struct incrementable_traits<T*>;
(2) (since C++20)
template< class T >

struct incrementable_traits<const T>

    : incrementable_traits<T> {};
(3) (since C++20)
template< class T >

    requires requires { typename T::difference_type; }

struct incrementable_traits<T>;
(4) (since C++20)
template< class T >

    requires (!requires { typename T::difference_type; }) &&
             requires(const T& a, const T& b) { { a - b } -> std::integral; }

struct incrementable_traits<T>;
(5) (since C++20)

Computes the associated difference type of the type I, if any. A program may specialize incrementable_traits for a program-defined type.

1) Primary template is an empty struct.
2) Specialization for pointers.
Provides a member type difference_type same as std::ptrdiff_t.
3) Specialization for const-qualified types.
4) Specialization for types that define a public and accessible member type difference_type.
Provides a member type difference_type same as T::difference_type.
5) Specialization for types that do not define a public and accessible member type difference_type but do support subtraction.
Provides a member type difference_type same as std::make_signed_t<decltype(std::declval<T>() - std::declval<T>())>. The implicit expression variations rule (see below) applies to the expression a - b.

[edit] Implicit expression variations

A requires expression that uses an expression that is non-modifying for some constant lvalue operand also requires implicit expression variations.

[edit] Example

[edit] See also

specifies that a semiregular type can be incremented with pre- and post-increment operators
(concept) [edit]
computes the associated types of an iterator
(alias template)[edit]
provides uniform interface to the properties of an iterator
(class template) [edit]