C 17 中自動模板參數推導的優點
C 17 引入了
用於模板實例化的auto 的自然擴展
與用於變量聲明的auto 關鍵字類似,
auto v1 = constant; // v1 == 5, decltype(v1) is int auto v2 = constant<true>; // v2 == true, decltype(v2) is bool auto v3 = constant; // v3 == 'a', decltype(v3) is char</true>
與用於變數宣告的auto 關鍵字類似,
自動>模板參數中的允許您在實例化時推斷非類型參數的類型。它消除了明確指定參數類型的需要,如下例所示:template <typename type value> constexpr Type constant = value; constexpr auto const IntConstant42 = constant<int>;</int></typename>
增強便利
template <auto value> constexpr auto constant = value; constexpr auto const IntConstant42 = constant;</auto>
用
template <auto ... vs> struct HeterogenousValueList {}; using MyList1 = HeterogenousValueList; template <auto v0 decltype ... vs> struct HomogenousValueList {}; using MyList2 = HomogenousValueList;</auto></auto>
提高程式碼簡潔性
以上是C 17 的 `auto` 關鍵字如何簡化模板參數推導?的詳細內容。更多資訊請關注PHP中文網其他相關文章!