7 Features of C++17 that will simplify your code
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Declaring non-type template parameters with auto
This is another part of the strategy to use auto
everywhere. With C++11 and C++14 you can use it to automatically deduce variables or even return types, plus there are also generic lambdas. Now you can also use it for deducing non-type template parameters.
For example:
template <auto value> void f() { }
f<10>(); // deduces int
This is useful, as you don't have to have a separate parameter for the type of non-type parameter. Like:
template <typename Type, Type value> constexpr Type TConstant = value;
// ^^^^ ^^^^
constexpr auto const MySuperConst = TConstant<int, 100>;
with C++17 it's a bit simpler:
template <auto value> constexpr auto TConstant = value;
// ^^^^
constexpr auto const MySuperConst = TConstant <100>;
So no need to write Type
explicitly.
As one of the advanced uses a lot of papers/blogs/talks point to an example of Heterogeneous compile time list:
template <auto ... vs> struct HeterogenousValueList {};
using MyList = HeterogenousValueList<'a', 100, 'b'>;
Before C++17 it was not possible to declare such list directly, some wrapper class would have to be provided first.
More details in
- P0127R2 - Declaring non-type template parameters with auto
- P0127R1 - Declaring non-type template arguments with auto - motivation, examples, discussion.
- c++ - Advantages of auto in template parameters in C++17 - Stack Overflow
- Trip report: Summer ISO C++ standards meeting (Oulu) | Sutter�s Mill
MSVC not yet, GCC: 7.0, Clang: 4.0.