Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/header/utility"

From cppreference.com
< cpp‎ | header
(Add exchange (C++14))
m (fmt.)
 
(33 intermediate revisions by 14 users not shown)
Line 6: Line 6:
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc h1|Includes}}
 
{{dsc h1|Includes}}
{{dsc| {{header|initializer_list}}{{mark c++11}}}}
+
{{dsc inc|cpp/header/dsc compare}}
 +
{{dsc inc|cpp/header/dsc initializer_list}}
 
{{dsc h1|Namespaces}}
 
{{dsc h1|Namespaces}}
{{dsc | {{ttb|[[cpp/utility/rel_ops/operator_cmp|rel_ops]]}} | Provide automatic comparison operators }}
+
{{dsc|{{ttb|[[cpp/utility/rel_ops/operator_cmp|rel_ops]]}}|Provide automatic comparison operators}}
{{dsc namespace | std::rel_ops }}
+
{{dsc namespace|std::rel_ops}}
{{dsc inc | cpp/utility/rel_ops/dsc operator_cmp}}
+
{{dsc inc|cpp/utility/rel_ops/dsc operator_cmp}}
 
{{dsc h1|Functions}}
 
{{dsc h1|Functions}}
{{dsc inc | cpp/algorithm/dsc swap}}
+
{{dsc inc|cpp/algorithm/dsc swap}}
{{dsc inc | cpp/utility/dsc exchange}}
+
{{dsc inc|cpp/utility/dsc exchange}}
{{dsc inc | cpp/utility/dsc forward}}
+
{{dsc inc|cpp/utility/dsc forward}}
{{dsc inc | cpp/utility/dsc move}}
+
{{dsc inc|cpp/utility/dsc forward_like}}
{{dsc inc | cpp/utility/dsc move_if_noexcept}}
+
{{dsc inc|cpp/utility/dsc move}}
{{dsc inc | cpp/utility/dsc declval}}
+
{{dsc inc|cpp/utility/dsc move_if_noexcept}}
{{dsc inc | cpp/utility/pair/dsc make_pair}}
+
{{dsc inc|cpp/utility/dsc as_const}}
{{dsc inc | cpp/utility/pair/dsc operator_cmp}}
+
{{dsc inc|cpp/utility/dsc declval}}
{{dsc inc | cpp/utility/pair/dsc swap2}}
+
{{dsc inc|cpp/utility/dsc to_underlying}}
{{dsc inc | cpp/utility/pair/dsc get}}
+
{{dsc inc|cpp/utility/dsc intcmp}}
 +
{{dsc inc|cpp/utility/dsc in_range}}
 +
{{dsc inc|cpp/utility/dsc unreachable}}
 +
{{dsc inc|cpp/utility/pair/dsc make_pair}}
 +
{{dsc inc|cpp/utility/pair/dsc operator_cmp}}
 +
{{dsc inc|cpp/utility/pair/dsc swap2}}
 +
{{dsc inc|cpp/utility/pair/dsc get}}
 
{{dsc h1|Classes}}
 
{{dsc h1|Classes}}
{{dsc inc | cpp/utility/dsc pair}}
+
{{dsc inc|cpp/utility/dsc pair}}
{{dsc inc | cpp/utility/dsc piecewise_construct_t}}
+
{{dsc inc|cpp/utility/dsc tuple_size}}
{{dsc inc | cpp/utility/dsc integer_sequence}}
+
{{dsc inc|cpp/utility/dsc tuple_element}}
 +
{{dsc inc|cpp/utility/pair/dsc tuple_size}}
 +
{{dsc inc|cpp/utility/pair/dsc tuple_element}}
 +
{{dsc inc|cpp/utility/dsc integer_sequence}}
 
{{dsc h2|Forward declarations}}
 
{{dsc h2|Forward declarations}}
{{dsc inc | cpp/utility/dsc tuple}}
+
{{dsc header|tuple}}
{{dsc h2|Objects}}
+
{{dsc inc|cpp/utility/dsc tuple}}
{{dsc inc | cpp/utility/dsc piecewise_construct}}
+
{{dsc h1|Constants}}
 +
{{dsc inc|cpp/utility/tuple/dsc ignore}}
 +
{{dsc h1|Tags}}
 +
{{dsc inc|cpp/utility/dsc piecewise_construct}}
 +
{{dsc inc|cpp/utility/dsc in_place}}
 +
{{dsc inc|cpp/utility/dsc nontype}}
 
{{dsc end}}
 
{{dsc end}}
 
 
  
 
===Synopsis===
 
===Synopsis===
{{source|
+
{{cpp/synopsis/utility}}
 
+
#include <initializer_list>
+
 
+
namespace std {
+
    // operators:
+
    namespace rel_ops {
+
        template<class T> bool operator!{{=}}(const T&, const T&);
+
        template<class T> bool operator> (const T&, const T&);
+
        template<class T> bool operator<{{=}}(const T&, const T&);
+
        template<class T> bool operator>{{=}}(const T&, const T&);
+
    }
+
 
+
    // swap:
+
    template<class T> void swap(T& a, T& b) noexcept(noexcept(a.swap(b));
+
    template <class T, size_t N>
+
        void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
+
 
+
    // exchange:
+
    template<class T, class U=T> T exchange(T& obj, U&& new_value);
+
 
+
    // forward/move:
+
    template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;
+
    template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept;
+
    template <class T> typename remove_reference<T>::type&& move(T&&) noexcept;
+
    template <class T> typename conditional<
+
        !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
+
        const T&, T&&>::type move_if_noexcept(T& x) noexcept;
+
 
+
    // declval, as unevaluated operand:
+
    template <class T>
+
        typename add_rvalue_reference<T>::type declval() noexcept;
+
+
    // pairs:
+
    template <class T1, class T2> struct pair;
+
 
+
    // pair specialized algorithms:
+
    template <class T1, class T2>
+
        bool operator{{{{=}}{{=}}}}(const pair<T1,T2>&, const
+
    template <class T1, class T2>
+
        bool operator< (const pair<T1,T2>&, const pair<T1,T2>&);
+
    template <class T1, class T2>
+
        bool operator!{{=}}(const pair<T1,T2>&, const pair<T1,T2>&);
+
    template <class T1, class T2>
+
        bool operator> (const pair<T1,T2>&, const pair<T1,T2>&);
+
    template <class T1, class T2>
+
        bool operator>{{=}}(const pair<T1,T2>&, const pair<T1,T2>&);
+
    template <class T1, class T2>
+
        bool operator<{{=}}(const pair<T1,T2>&, const pair<T1,T2>&);
+
    template <class T1, class T2>
+
        void swap(pair<T1,T2>& x, pair<T1,T2>& y) noexcept(noexcept(x.swap(y)));
+
    template <class T1, class T2>
+
        pair<V1,V2> make_pair(T1&&, T2&&);
+
 
+
    //tuple-like access to pair:
+
    template <class T> class tuple_size;
+
    template <size_t I, class T> class tuple_element;
+
   
+
    template <class T1, class T2> struct tuple_size<std::pair<T1, T2> >;
+
    template <class T1, class T2> struct tuple_element<0, std::pair<T1, T2> >;
+
    template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> >;
+
   
+
    template<size_t I, class T1, class T2>
+
        typename tuple_element<I, std::pair<T1, T2> >::type&
+
        get(std::pair<T1, T2>&) noexcept;
+
    template<size_t I, class T1, class T2>
+
        typename tuple_element<I, std::pair<T1, T2> >::type&&
+
        get(std::pair<T1, T2>&&) noexcept;
+
    template<size_t I, class T1, class T2>
+
        const typename tuple_element<I, std::pair<T1, T2> >::type&
+
            get(const std::pair<T1, T2>&) noexcept;
+
           
+
    // pair piecewise construction
+
    struct piecewise_construct_t { };
+
    constexpr piecewise_construct_t piecewise_construct {{=}} piecewise_construct_t();
+
 
+
    template <class... Types> class tuple; // defined in <tuple>
+
}
+
 
+
 
+
}}
+
 
+
====Class {{lc|std::pair}}====
+
{{source|
+
 
+
template <class T1, class T2>
+
struct pair {
+
    typedef T1 first_type;
+
    typedef T2 second_type;
+
   
+
    T1 first;
+
    T2 second;
+
    pair(const pair&) {{=}} default;
+
    pair(pair&&) {{=}} default;
+
 
+
    constexpr pair();
+
    pair(const T1& x, const T2& y);
+
    template<class U, class V> pair(U&& x, V&& y);
+
    template<class U, class V> pair(const pair<U, V>& p);
+
    template<class U, class V> pair(pair<U, V>&& p);
+
    template <class... Args1, class... Args2>
+
        pair(piecewise_construct_t,
+
            tuple<Args1...> first_args, tuple<Args2...> second_args);
+
       
+
    pair& operator{{=}}(const pair& p);
+
    template<class U, class V> pair& operator{{=}}(const pair<U, V>& p);
+
    pair& operator{{=}}(pair&& p) noexcept(see below);
+
    template<class U, class V> pair& operator{{=}}(pair<U, V>&& p);
+
   
+
    void swap(pair& p) noexcept( noexcept(swap(first, p.first)) &&
+
                                noexcept(swap(second, p.second)) );
+
};
+
 
+
}}
+
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc| {{header|tuple}} | Defines {{lc|std::tuple}} }}
+
{{dsc inc|cpp/header/dsc tuple}}
{{dsc| [[cpp/utility|Utility library]] }}
+
 
{{dsc end}}
 
{{dsc end}}
 +
 +
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 13:11, 24 July 2024

 
 
Standard library headers
General utilities
<any> (C++17)
<bitset>
<bit> (C++20)
<charconv> (C++17)
<expected> (C++23)
<format> (C++20)
<functional>
<optional> (C++17)
<tuple> (C++11)
<typeindex> (C++11)
<utility>
<variant> (C++17)
Containers
<array> (C++11)
<deque>
<flat_map> (C++23)
<flat_set> (C++23)
<forward_list> (C++11)
<inplace_vector> (C++26)   
<list>
<map>
<mdspan> (C++23)
<queue>
<set>
<span> (C++20)
<stack>
<unordered_map> (C++11)
<unordered_set> (C++11)
<vector>
Iterators
<iterator>
Ranges
<generator> (C++23)
<ranges> (C++20)
 

This header is part of the general utility library.

Contents

Includes

(C++20)
Three-way comparison operator support[edit]
std::initializer_list class template[edit]

Namespaces

rel_ops Provide automatic comparison operators
Defined in namespace std::rel_ops
automatically generates comparison operators based on user-defined operator== and operator<
(function template) [edit]

Functions

swaps the values of two objects
(function template) [edit]
(C++14)
replaces the argument with a new value and returns its previous value
(function template) [edit]
(C++11)
forwards a function argument and use the type template argument to preserve its value category
(function template) [edit]
forwards a function argument as if casting it to the value category and constness of the expression of specified type template argument
(function template) [edit]
(C++11)
converts the argument to an xvalue
(function template) [edit]
converts the argument to an xvalue if the move constructor does not throw
(function template) [edit]
(C++17)
obtains a reference to const to its argument
(function template) [edit]
(C++11)
obtains a reference to an object of the template type argument for use in an unevaluated context
(function template) [edit]
converts an enumeration to its underlying type
(function template) [edit]
compares two integer values, ensuring that signed negative numbers are less than unsigned numbers
(function template) [edit]
(C++20)
checks if an integer value is in the range of a given integer type
(function template) [edit]
marks unreachable point of execution
(function) [edit]
creates a pair object of type, determined by the argument types
(function template) [edit]
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
lexicographically compares the values in the pair
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
accesses an element of a pair
(function template) [edit]

Classes

implements binary tuple, i.e. a pair of values
(class template) [edit]
obtains the number of elements of a tuple-like type
(class template) [edit]
obtains the element types of a tuple-like type
(class template) [edit]
obtains the size of a pair
(class template specialization) [edit]
obtains the type of the elements of pair
(class template specialization) [edit]
implements compile-time sequence of integers
(class template) [edit]
Forward declarations
Defined in header <tuple>
(C++11)
implements fixed size container, which holds elements of possibly different types
(class template) [edit]

Constants

(C++11)
placeholder to skip an element when unpacking a tuple using tie
(constant) [edit]

Tags

piecewise construction tag
(tag)[edit]
in-place construction tag
(tag)[edit]
value construction tag
(tag)[edit]

[edit] Synopsis

#include <compare>
#include <initializer_list>
 
namespace std {
  // swap
  template<class T>
    constexpr void swap(T& a, T& b) noexcept(/* see description */);
  template<class T, size_t N>
    constexpr void swap(T (&a)[N], T (&b)[N]) noexcept(is_nothrow_swappable_v<T>);
 
  // exchange
  template<class T, class U = T>
    constexpr T exchange(T& obj, U&& new_val);
 
  // forward/move
  template<class T>
    constexpr T&& forward(remove_reference_t<T>& t) noexcept;
  template<class T>
    constexpr T&& forward(remove_reference_t<T>&& t) noexcept;
  template<class T, class U>
    constexpr /* see description */ forward_like(U&& x) noexcept;
  template<class T>
    constexpr remove_reference_t<T>&& move(T&&) noexcept;
  template<class T>
    constexpr conditional_t<
        !is_nothrow_move_constructible_v<T> && is_copy_constructible_v<T>, const T&, T&&>
      move_if_noexcept(T& x) noexcept;
 
  // as_const
  template<class T>
    constexpr add_const_t<T>& as_const(T& t) noexcept;
  template<class T>
    void as_const(const T&&) = delete;
 
  // declval
  template<class T>
    add_rvalue_reference_t<T> declval() noexcept;   // as unevaluated operand
 
  // integer comparison functions
  template<class T, class U>
    constexpr bool cmp_equal(T t, U u) noexcept;
  template<class T, class U>
    constexpr bool cmp_not_equal(T t, U u) noexcept;
 
  template<class T, class U>
    constexpr bool cmp_less(T t, U u) noexcept;
  template<class T, class U>
    constexpr bool cmp_greater(T t, U u) noexcept;
  template<class T, class U>
    constexpr bool cmp_less_equal(T t, U u) noexcept;
  template<class T, class U>
    constexpr bool cmp_greater_equal(T t, U u) noexcept;
 
  template<class R, class T>
    constexpr bool in_range(T t) noexcept;
 
  // to_underlying
  template<class T>
    constexpr underlying_type_t<T> to_underlying(T value) noexcept;
 
  // unreachable
  [[noreturn]] void unreachable();
 
  // compile-time integer sequences
  template<class T, T...>
    struct integer_sequence;
  template<size_t... I>
    using index_sequence = integer_sequence<size_t, I...>;
 
  template<class T, T N>
    using make_integer_sequence = integer_sequence<T, /* see description */>;
  template<size_t N>
    using make_index_sequence = make_integer_sequence<size_t, N>;
 
  template<class... T>
    using index_sequence_for = make_index_sequence<sizeof...(T)>;
 
  // class template pair
  template<class T1, class T2>
    struct pair;
 
  // pair specialized algorithms
  template<class T1, class T2>
    constexpr bool operator==(const pair<T1, T2>&, const pair<T1, T2>&);
  template<class T1, class T2>
    constexpr common_comparison_category_t</*synth-three-way-result*/<T1>,
                                           /*synth-three-way-result*/<T2>>
      operator<=>(const pair<T1, T2>&, const pair<T1, T2>&);
 
  template<class T1, class T2>
    constexpr void swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
 
  template<class T1, class T2>
    constexpr /* see description */ make_pair(T1&&, T2&&);
 
  // tuple-like access to pair
  template<class T> struct tuple_size;
  template<size_t I, class T> struct tuple_element;
 
  template<class T1, class T2> struct tuple_size<pair<T1, T2>>;
  template<size_t I, class T1, class T2> struct tuple_element<I, pair<T1, T2>>;
 
  template<size_t I, class T1, class T2>
    constexpr tuple_element_t<I, pair<T1, T2>>& get(pair<T1, T2>&) noexcept;
  template<size_t I, class T1, class T2>
    constexpr tuple_element_t<I, pair<T1, T2>>&& get(pair<T1, T2>&&) noexcept;
  template<size_t I, class T1, class T2>
    constexpr const tuple_element_t<I, pair<T1, T2>>& get(const pair<T1, T2>&) noexcept;
  template<size_t I, class T1, class T2>
    constexpr const tuple_element_t<I, pair<T1, T2>>&& get(const pair<T1, T2>&&) noexcept;
  template<class T1, class T2>
    constexpr T1& get(pair<T1, T2>& p) noexcept;
  template<class T1, class T2>
    constexpr const T1& get(const pair<T1, T2>& p) noexcept;
  template<class T1, class T2>
    constexpr T1&& get(pair<T1, T2>&& p) noexcept;
  template<class T1, class T2>
    constexpr const T1&& get(const pair<T1, T2>&& p) noexcept;
  template<class T2, class T1>
    constexpr T2& get(pair<T1, T2>& p) noexcept;
  template<class T2, class T1>
    constexpr const T2& get(const pair<T1, T2>& p) noexcept;
  template<class T2, class T1>
    constexpr T2&& get(pair<T1, T2>&& p) noexcept;
  template<class T2, class T1>
    constexpr const T2&& get(const pair<T1, T2>&& p) noexcept;
 
  // pair piecewise construction
  struct piecewise_construct_t {
    explicit piecewise_construct_t() = default;
  };
  inline constexpr piecewise_construct_t piecewise_construct{};
  template<class... Types> class tuple;         // defined in <tuple>
 
  // in-place construction
  struct in_place_t {
    explicit in_place_t() = default;
  };
  inline constexpr in_place_t in_place{};
 
  template<class T>
    struct in_place_type_t {
      explicit in_place_type_t() = default;
    };
  template<class T> inline constexpr in_place_type_t<T> in_place_type{};
 
  template<size_t I>
    struct in_place_index_t {
      explicit in_place_index_t() = default;
    };
  template<size_t I> inline constexpr in_place_index_t<I> in_place_index{};
 
  // nontype argument tag
  template<auto V>
    struct nontype_t {
      explicit nontype_t() = default;
    };
  template<auto V> inline constexpr nontype_t<V> nontype{};
}
 
// deprecated
namespace std::rel_ops {
  template<class T> bool operator!=(const T&, const T&);
  template<class T> bool operator> (const T&, const T&);
  template<class T> bool operator<=(const T&, const T&);
  template<class T> bool operator>=(const T&, const T&);
}

[edit] Class template std::integer_sequence

namespace std {
  template<class T, T... I> struct integer_sequence {
    using value_type = T;
    static constexpr size_t size() noexcept { return sizeof...(I); }
  };
}

[edit] Class template std::pair

namespace std {
  template<class T1, class T2>
  struct pair {
    using first_type  = T1;
    using second_type = T2;
 
    T1 first;
    T2 second;
 
    pair(const pair&) = default;
    pair(pair&&) = default;
    constexpr explicit(/* see description */) pair();
    constexpr explicit(/* see description */) pair(const T1& x, const T2& y);
    template<class U1 = T1, class U2 = T2>
      constexpr explicit(/* see description */) pair(U1&& x, U2&& y);
    template<class U1, class U2>
      constexpr explicit(/* see description */) pair(const pair<U1, U2>& p);
    template<class U1, class U2>
      constexpr explicit(/* see description */) pair(pair<U1, U2>&& p);
    template<class... Args1, class... Args2>
      constexpr pair(piecewise_construct_t,
                     tuple<Args1...> first_args, tuple<Args2...> second_args);
 
    constexpr pair& operator=(const pair& p);
    template<class U1, class U2>
      constexpr pair& operator=(const pair<U1, U2>& p);
    constexpr pair& operator=(pair&& p) noexcept(/* see description */);
    template<class U1, class U2>
      constexpr pair& operator=(pair<U1, U2>&& p);
 
    constexpr void swap(pair& p) noexcept(/* see description */);
  };
 
  template<class T1, class T2>
    pair(T1, T2) -> pair<T1, T2>;
}

[edit] See also

(C++11)
std::tuple class template[edit]