名稱空間
變體
操作

std::common_type

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

common_type
(C++11)
(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <type_traits>
template< class... T >
struct common_type;
(C++11 起)

確定所有型別 T... 中的通用型別,即所有 T... 都可以顯式轉換為的型別。如果存在這樣的型別(根據以下規則確定),則成員 type 命名該型別。否則,沒有成員 type

  • 如果 sizeof...(T) 為零,則沒有成員 `type`。
  • 如果 sizeof...(T) 為一(即 T... 只包含一個型別 T0),則成員 type 命名與 std::common_type<T0, T0>::type 相同的型別(如果存在);否則沒有成員 type
  • 如果 sizeof...(T) 為二(即 T... 恰好包含兩個型別 T1T2),則
  • 如果對 T1T2 中至少一個應用 std::decay 產生不同的型別,則成員 type 命名與 std::common_type<std::decay<T1>::type, std::decay<T2>::type>::type 相同的型別(如果存在);如果不存在,則沒有成員 type
  • 否則,如果存在 std::common_type<T1, T2> 的使用者特化,則使用該特化;
  • 否則,如果 std::decay<decltype(false ? std::declval<T1>() : std::declval<T2>())>::type 是有效型別,則成員 type 表示該型別,參見 條件運算子
(C++20 起)
  • 否則,沒有成員 type
  • 如果 sizeof...(T) 大於二(即 T... 由型別 T1, T2, R... 組成),則如果 std::common_type<T1, T2>::type 存在,則成員 type 表示 std::common_type<typename std::common_type<T1, T2>::type, R...>::type(如果存在這樣的型別)。在所有其他情況下,沒有成員 type

如果引數包 T 中的任何型別不是完整型別、(可能帶 cv 限定符的)void,或未知邊界的陣列,則行為未定義。

如果上述模板的例項化直接或間接依賴於不完整型別,並且該例項化在該型別假設完成時可能產生不同的結果,則行為未定義。

目錄

[編輯] 巢狀型別

名稱 定義
型別 所有 T 的通用型別

[編輯] 輔助型別

template< class... T >
using common_type_t = typename common_type<T...>::type;
(C++14 起)

[編輯] 特化

使用者可以為型別 T1T2 特化 common_type,如果

  • T1T2 中至少有一個依賴於使用者定義型別,並且
  • std::decayT1T2 都是恆等變換。

如果這樣的特化具有名為 type 的成員,它必須是公共且明確的成員,命名一個 cv 無限定的非引用型別,並且 T1T2 都可以顯式轉換為該型別。此外,std::common_type<T1, T2>::typestd::common_type<T2, T1>::type 必須表示相同的型別。

違反這些規則新增 common_type 特化的程式的行為是未定義的。

請注意,向 <type_traits> 中任何其他模板 (除了 std::basic_common_reference 之外)(C++20 起) 新增特化的程式的行為是未定義的。

標準庫已提供以下特化:

特化 std::common_type 特徵
(類模板特化) [編輯]
特化 std::common_type 特徵
(類模板特化) [編輯]
確定兩個 pair 的公共型別
(類模板特化) [編輯]
確定 tupletuple-like 型別的通用型別
(類模板特化) [編輯]
確定迭代器和適配的 basic_const_iterator 型別的通用型別
(類模板特化) [編輯]

[編輯] 可能實現

// primary template (used for zero types)
template<class...>
struct common_type {};
 
// one type
template<class T>
struct common_type<T> : common_type<T, T> {};
 
namespace detail
{
    template<class...>
    using void_t = void;
 
    template<class T1, class T2>
    using conditional_result_t = decltype(false ? std::declval<T1>() : std::declval<T2>());
 
    template<class, class, class = void>
    struct decay_conditional_result {};
    template<class T1, class T2>
    struct decay_conditional_result<T1, T2, void_t<conditional_result_t<T1, T2>>>
        : std::decay<conditional_result_t<T1, T2>> {};
 
    template<class T1, class T2, class = void>
    struct common_type_2_impl : decay_conditional_result<const T1&, const T2&> {};
 
    // C++11 implementation:
    // template<class, class, class = void>
    // struct common_type_2_impl {};
 
    template<class T1, class T2>
    struct common_type_2_impl<T1, T2, void_t<conditional_result_t<T1, T2>>>
        : decay_conditional_result<T1, T2> {};
}
 
// two types
template<class T1, class T2>
struct common_type<T1, T2> 
    : std::conditional<std::is_same<T1, typename std::decay<T1>::type>::value &&
                       std::is_same<T2, typename std::decay<T2>::type>::value,
                       detail::common_type_2_impl<T1, T2>,
                       common_type<typename std::decay<T1>::type,
                                   typename std::decay<T2>::type>>::type {};
 
// 3+ types
namespace detail
{
    template<class AlwaysVoid, class T1, class T2, class... R>
    struct common_type_multi_impl {};
    template<class T1, class T2, class...R>
    struct common_type_multi_impl<void_t<typename common_type<T1, T2>::type>, T1, T2, R...>
        : common_type<typename common_type<T1, T2>::type, R...> {};
}
 
template<class T1, class T2, class... R>
struct common_type<T1, T2, R...>
    : detail::common_type_multi_impl<void, T1, T2, R...> {};

[編輯] 注意

對於不進行型別提升的算術型別,通用型別可以看作是(可能混合模式的)算術表示式,例如 T0() + T1() + ... + Tn() 的型別。

[編輯] 示例

演示程式定義的類上的混合模式算術

#include <iostream>
#include <type_traits>
 
template<class T>
struct Number { T n; };
 
template<class T, class U>
constexpr Number<std::common_type_t<T, U>>
    operator+(const Number<T>& lhs, const Number<U>& rhs)
{
    return {lhs.n + rhs.n};
}
 
void describe(const char* expr, const Number<int>& x)
{
    std::cout << expr << "  is  Number<int>{" << x.n << "}\n";
}
 
void describe(const char* expr, const Number<double>& x)
{
    std::cout << expr << "  is  Number<double>{" << x.n << "}\n";
}
 
int main()
{
    Number<int> i1 = {1}, i2 = {2};
    Number<double> d1 = {2.3}, d2 = {3.5};
    describe("i1 + i2", i1 + i2);
    describe("i1 + d2", i1 + d2);
    describe("d1 + i2", d1 + i2);
    describe("d1 + d2", d1 + d2);
}

輸出

i1 + i2  is  Number<int>{3}
i1 + d2  is  Number<double>{4.5}
d1 + i2  is  Number<double>{4.3}
d1 + d2  is  Number<double>{5.8}

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2141 C++11 條件運算子的結果型別未衰減 衰減了結果型別
LWG 2408 C++11 common_type 不支援 SFINAE 變為 SFINAE 友好
LWG 2460 C++11 common_type 特化幾乎無法編寫 減少了所需的特化數量
特化數量

[編輯] 另見

指定兩種型別共享一個共同的型別
(概念) [編輯]