std::common_with
std::common_with (공통 타입 개념)
두 타입 T와 U가 공통 타입(std::common_type_t로 계산)을 공유하며 둘 다 그 타입으로 변환될 수 있는지를 명세하는 개념(concept)이에요. C++20부터 있어요.
출처: cppreference
본문
<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>>>;
개념 common_with<T, U>는 두 타입 T와 U가 공통 타입(std::common_type_t로 계산)을 공유하며 둘 다 그 타입으로 변환될 수 있음을 명세해요.
의미 요구사항
T와 U는 다음일 때만 std::common_with<T, U>를 모델링해요. 동등성 보존 표현식 t1, t2, u1, u2가 주어지고 decltype((t1))·decltype((t2))가 모두 T이며 decltype((u1))·decltype((u2))가 모두 U일 때,
std::common_type_t<T, U>(t1)이std::common_type_t<T, U>(t2)와 같음은t1이t2와 같을 때와 그때만; 그리고std::common_type_t<T, U>(u1)이std::common_type_t<T, U>(u2)와 같음은u1이u2와 같을 때와 그때만.
즉 공통 타입으로의 변환이 동등성을 보존해야 해요.
common_with는 assignable_from 등 다른 개념의 구성 요소로 자주 쓰여요. 두 타입이 공통 타입을 가질 때 그 공통 타입에 대한 변환의 정합성까지 요구하므로, 단순히 std::common_type이 형성되는지보다 더 강한 조건을 검사해요.
참고 문헌
- C++23 표준 (ISO/IEC 14882:2024): 18.4.6 Concept common_with [concept.common]
- C++20 표준 (ISO/IEC 14882:2020): 18.4.6 Concept common_with [concept.common]