Advantages of Automatic Template Parameter Deduction in C 17
C 17 introduces the
Natural Extension of auto for Template Instantiation
Similar to the auto keyword used for variable declaration,
auto v1 = constant<5>; // v1 == 5, decltype(v1) is int auto v2 = constant<true>; // v2 == true, decltype(v2) is bool auto v3 = constant<'a'>; // v3 == 'a', decltype(v3) is char
Enhanced Convenience
Replacing explicit type declarations with
template <typename Type, Type value> constexpr Type constant = value; constexpr auto const IntConstant42 = constant<int, 42>;
This code can be rewritten with
template <auto value> constexpr auto constant = value; constexpr auto const IntConstant42 = constant<42>;
Improved Code Conciseness
template <auto ... vs> struct HeterogenousValueList {}; using MyList1 = HeterogenousValueList<42, 'X', 13u>; template <auto v0, decltype(v0) ... vs> struct HomogenousValueList {}; using MyList2 = HomogenousValueList<1, 2, 3>;
In comparison, achieving the same functionality in pre-C 17 would require more verbose and convoluted constructs involving additional templates.
The above is the detailed content of How Does C 17's `auto` Keyword Simplify Template Parameter Deduction?. For more information, please follow other related articles on the PHP Chinese website!