In the example below, we use the C++17 feature "Class template argument deduction" to deduce that val
is of type Base<int, double, bool>
:
template<class T, class U, class V>
struct Base {
Base(T, U) { };
Base(T, U, V) { };
Base(V) { };
};
void func() {
Base val(1, 4., false);
}
Now, is it possible to partially specify the template arguments, and let the remaining ones be deduced? Effectively something like this:
Base<V = bool> val1(1, 4.); // U & V deduced --> Base<int, double, bool>
Base<T = bool, T = int> val2(5.); // V deduced --> Base<bool, int, double>
I've tried e.g.
template<class T, class U> using Base2 = Base<T, U, double>;
void func() {
NewBase2 val(1, 2);
}
but it doesn't compile: 'Base2': use of alias template requires template argument list
.
Is partial deduction possible somehow? If it is not possible directly, are there any good workarounds?
T
and haveU
and haveV
deduced, or specifyT
andU
, and haveV
deduced. See the pattern? It would be nice to have named template parameters in C++, unfortunately we do not. – Engagement