Namespaces
Variants

Standard library header <utility>

From cppreference.net
Standard library headers

此头文件属于 通用工具 库。

目录

包含文件

(C++20)
三路比较运算符 支持
std::initializer_list 类模板

命名空间

rel_ops 提供自动比较运算符
定义于命名空间 std::rel_ops
基于用户定义的 operator == operator < 自动生成比较运算符
(函数模板)

函数

交换两个对象的值
(函数模板)
(C++14)
用新值替换参数并返回其先前值
(函数模板)
(C++11)
转发函数参数并使用类型模板参数保留其值类别
(函数模板)
转发函数参数,如同将其转换为指定类型模板参数表达式的值类别和常量性
(函数模板)
(C++11)
将参数转换为亡值
(函数模板)
如果移动构造函数不抛出异常,则将参数转换为亡值
(函数模板)
(C++17)
获取其参数的 const 引用
(函数模板)
(C++11)
获取模板类型参数对象的引用,用于不求值上下文
(函数模板)
将枚举转换为其底层类型
(函数模板)
比较两个整数值,确保有符号负数小于无符号数
(函数模板)
(C++20)
检查整数值是否在给定整数类型的范围内
(函数模板)
标记不可达的执行点
(函数)
<a href="../utility/pair/make_pair.html" title="cpp/utility

概要

#include <compare>
#include <initializer_list>
namespace std {
  // 交换
  template<class T>
    constexpr void swap(T& a, T& b) noexcept(/* 参见描述 */);
  template<class T, size_t N>
    constexpr void swap(T (&a)[N], T (&b)[N]) noexcept(is_nothrow_swappable_v<T>);
  // 交换并返回原值
  template<class T, class U = T>
    constexpr T exchange(T& obj, U&& new_val);
  // 完美转发/移动
  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 /* 参见描述 */ 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;
  // 转换为常量引用
  template<class T>
    constexpr add_const_t<T>& as_const(T& t) noexcept;
  template<class T>
    void as_const(const T&&) = delete;
  // 获取类型声明值
  template<class T>
    add_rvalue_reference_t<T> declval() noexcept;   // 作为未求值操作数
  // 整数比较函数
  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;
  // 转换为底层类型
  template<class T>
    constexpr underlying_type_t<T> to_underlying(T value) noexcept;
  // 不可达标记
  [[noreturn]] void unreachable();
  // 编译期整数序列
  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, /* 参见描述 */>;
  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)>;
  // 类模板 pair
  template<class T1, class T2>
    struct pair;
  // pair 特化算法
  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<<span class="co

类模板 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); }
  };
}

类模板 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(/* 参见描述 */) pair();
    constexpr explicit(/* 参见描述 */) pair(const T1& x, const T2& y);
    template<class U1 = T1, class U2 = T2>
      constexpr explicit(/* 参见描述 */) pair(U1&& x, U2&& y);
    template<class U1, class U2>
      constexpr explicit(/* 参见描述 */) pair(const pair<U1, U2>& p);
    template<class U1, class U2>
      constexpr explicit(/* 参见描述 */) 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(/* 参见描述 */);
    template<class U1, class U2>
      constexpr pair& operator=(pair<U1, U2>&& p);
    constexpr void swap(pair& p) noexcept(/* 参见描述 */);
  };
  template<class T1, class T2>
    pair(T1, T2) -> pair<T1, T2>;
}

参见

(C++11)
std::tuple 类模板