std::common_with - cppreference.com

std::common_with

来自cppreference.com
在标头 <concepts> 定义
template< class T, class U >
concept common_with =
    std::same_as<std::common_type_t<T, U>, std::common_type_t<U, T>> &&
    requires {
        static_cast<std::common_type_t<T, U>>(std::declval<T>());
        static_cast<std::common_type_t<T, U>>(std::declval<U>());
    } &&
    std::common_reference_with<
        std::add_lvalue_reference_t<const T>,
        std::add_lvalue_reference_t<const U>> &&
    std::common_reference_with<
        std::add_lvalue_reference_t<std::common_type_t<T, U>>,
        std::common_reference_t<
            std::add_lvalue_reference_t<const T>,
            std::add_lvalue_reference_t<const U>>>;
(C++20 起)

概念 common_with<T, U> 指定两个类型 TU 共享一种二者均能转换到的公共类型(以 std::common_type_t 计算)。

语义要求

T 与 U 实现 std::common_with<T, U>,仅当给定保持相等性的表达式 t1t2u1u2,且它们满足 decltype((t1))decltype((t2)) 均为 Tdecltype((u1))decltype((u2)) 均为 U,且满足以下条件

  • 当且仅当 t1 等于 t2std::common_type_t<T, U>(t1) 等于 std::common_type_t<T, U>(t2);且
  • 当且仅当 u1 等于 u2std::common_type_t<T, U>(u1) 等于 std::common_type_t<T, U>(u2)

换言之,到公共类型的转换必须保持相等性

相等性保持

标准库概念的 requires 表达式中声明的表达式都要求保持相等性(除非另外说明)。

参阅