标准库标头 <utility>
来自cppreference.com
此头文件是通用工具库的一部分。
包含 | |
(C++20) |
三路比较运算符支持 |
(C++11) |
std::initializer_list 类模板 |
命名空间 | |
rel_ops
|
提供自动的比较运算符 |
在命名空间
std::rel_ops 定义 | |
(C++20 中弃用) |
基于用户定义的 operator== 和 operator< 自动生成比较运算符 (函数模板) |
函数 | |
| 交换两个对象的值 (函数模板) | |
(C++14) |
替换实参为一个新值,并返回它先前的值 (函数模板) |
(C++11) |
转发一个函数实参,并使用模板实参保留它的值类别 (函数模板) |
(C++23) |
转发函数实参,如同将它转换为指定的类型模板实参的表达式的值类别与 const 性 (函数模板) |
(C++11) |
转换实参为亡值 (函数模板) |
(C++11) |
若移动构造函数不抛出则转换实参为亡值 (函数模板) |
(C++17) |
获得到其实参的 const 引用 (函数模板) |
(C++11) |
在不求值语境中获取到模板类型实参的对象的引用 (函数模板) |
(C++23) |
转换枚举为其底层类型 (函数模板) |
| 比较两个整数,保证有符号负数小于无符号数 (函数模板) | |
(C++20) |
检查整数是否在给定整数类型的范围内 (函数模板) |
(C++23) |
标记执行的不可抵达点 (函数) |
创建一个 pair 对象,其类型根据各实参类型确定 (函数模板) | |
(C++20 移除)(C++20 移除)(C++20 移除)(C++20 移除)(C++20 移除)(C++20) |
按字典序比较 pair 中的值 (函数模板) |
(C++11) |
特化 std::swap 算法 (函数模板) |
(C++11) |
访问 pair 的一个元素 (函数模板) |
类 | |
| 实现二元组,即一对值 (类模板) | |
(C++11) |
获得元组式类型的元素数量 (类模板) |
(C++11) |
获得元组式类型的元素类型 (类模板) |
(C++11) |
获得 pair 的大小 (类模板特化) |
获得 pair 中元素的类型 (类模板特化) | |
(C++14) |
实现编译时整数数列 (类模板) |
前置声明 | |
在标头
<tuple> 定义 | |
(C++11) |
实现固定大小的容器,可保有类型相异的元素 (类模板) |
在标头
<variant> 定义 | |
(C++17) |
用作非可默认构造类型的 variant 的首个可选项的占位符类型 (类) |
常量 | |
(C++11) |
用 tie 解包 tuple 时用来跳过元素的占位符 (常量) |
标签 | |
(C++11) |
用于为逐段构造选择正确函数重载的标签类型 (类) |
| 逐段构造标签 (标签) | |
| 原位构造标签 (类模板) | |
概要
#include <compare>
#include <initializer_list>
namespace std {
// swap
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>);
// 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 /* 见描述 */ 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; // 作为不求值操作数
// 整数比较函数
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();
// 编译时整数数列
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</*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 /* 见描述 */ make_pair(T1&&, T2&&);
// 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 逐片构造
struct piecewise_construct_t {
explicit piecewise_construct_t() = default;
};
inline constexpr piecewise_construct_t piecewise_construct{};
template<class... Types> class tuple; // 定义于 <tuple>
// 原位构造
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 实参标签
template<auto V>
struct nontype_t {
explicit nontype_t() = default;
};
template<auto V> inline constexpr nontype_t<V> nontype{};
}
// 弃用
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&);
}
类模板 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 类模板 |