C++ - auto as a non-type parameter in template
C++17 and later: auto as a non-type template parameter
Starting from C++17, we can use auto as a non-type template parameter but only in certain contexts. Specifically, we can use auto to deduce the type of a non-type template parameter, like this:
1
2
3
template <auto Value>
void function() {
}
In this case, Value is a non-type template parameter which type is deduced by the compiler based on the initializer expression. For example:
1
2
function<5>(); // Value is deduced to be an int
function<'a'>(); // Value is deduced to be a char
C++20, we can use a lambda expression as a non-type template parameter like this:
1
2
3
4
5
6
7
8
template <auto F>
void f() {
F(5);
}
int main() {
f<[](int x) { std::cout << x; }>(); // prints 5
}