Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/concepts/common with"

From cppreference.com
< cpp‎ | concepts
m (Added Spanish language link)
Line 20: Line 20:
 
The concept {{tt|common_with<T, U>}} specifies that two types {{tt|T}} and {{tt|U}} share a ''common type'' (as computed by {{lc|std::common_type_t}}) to which both can be converted.
 
The concept {{tt|common_with<T, U>}} specifies that two types {{tt|T}} and {{tt|U}} share a ''common type'' (as computed by {{lc|std::common_type_t}}) to which both can be converted.
  
 +
=== Semantic requirements ===
 
T and U model {{tt|common_with<T, U>}} only if, given equality-preserving expressions {{tt|t1}}, {{tt|t2}}, {{tt|u1}} and {{tt|u2}} such that {{c|decltype((t1))}} and {{c|decltype((t2))}} are both {{tt|T}} and {{c|decltype((u1))}} and {{c|decltype((u2))}} are both {{tt|U}},
 
T and U model {{tt|common_with<T, U>}} only if, given equality-preserving expressions {{tt|t1}}, {{tt|t2}}, {{tt|u1}} and {{tt|u2}} such that {{c|decltype((t1))}} and {{c|decltype((t2))}} are both {{tt|T}} and {{c|decltype((u1))}} and {{c|decltype((u2))}} are both {{tt|U}},
 
* {{c|std::common_type_t<T, U>(t1)}} equals {{c|std::common_type_t<T, U>(t2)}} if and only if {{tt|t1}} equals {{tt|t2}}; and
 
* {{c|std::common_type_t<T, U>(t1)}} equals {{c|std::common_type_t<T, U>(t2)}} if and only if {{tt|t1}} equals {{tt|t2}}; and

Revision as of 15:44, 22 February 2020

Defined in header <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>>>;
(since C++20)

The concept common_with<T, U> specifies that two types T and U share a common type (as computed by std::common_type_t) to which both can be converted.

Semantic requirements

T and U model common_with<T, U> only if, given equality-preserving expressions t1, t2, u1 and u2 such that decltype((t1)) and decltype((t2)) are both T and decltype((u1)) and decltype((u2)) are both U,

In other words, the conversion to the common type must preserve equality.

Equality preservation

Expressions declared in requires expressions of the standard library concepts are required to be equality-preserving (except where stated otherwise).

See also

determines the common type of a group of types
(class template) [edit]
determines the common reference type of a group of types
(class template) [edit]
specifies that two types share a common reference type
(concept) [edit]