Phi*_*ZXX 2 c++ class-template template-argument-deduction c++17
在下面的示例中,我们使用C ++ 17功能“类模板参数推导”来推断val类型为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);
}
Run Code Online (Sandbox Code Playgroud)
现在,是否可以部分指定模板参数,然后推导出其余参数?实际上是这样的:
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>
Run Code Online (Sandbox Code Playgroud)
我试过了
template<class T, class U> using Base2 = Base<T, U, double>;
void func() {
NewBase2 val(1, 2);
}
Run Code Online (Sandbox Code Playgroud)
但无法编译:'Base2': use of alias template requires template argument list。
是否可以以某种方式进行部分扣除?如果不可能直接解决,有什么好的解决方法?
您可以添加扣除指南如下:
template<class T, class U, class V>
Base(T, U) -> Base<T, U, bool>;
template<class V>
Base(V) -> Base<bool, int, V>;
Run Code Online (Sandbox Code Playgroud)
这使得
Base val1(1, 4.); // Base<int, double, bool>
Base val2(5.); // Base<bool, int, double>
Run Code Online (Sandbox Code Playgroud)
如果要指定“默认”模板,可以使用旧方法 make_
template <typename V, typename T, typename U>
Base<T, U, V> make_Base(T t, U u)
{
return Base<T, U, V>{t, u};
}
template <typename T, typename U, typename V>
Base<T, U, V> make_Base(V v)
{
return Base<T, U, V>{v};
}
auto val1 = make_Base<bool>(1, 4.); // Base<int, double, bool>
auto val2 = make_Base<bool, int>(5.); // Base<bool, int, double>
Run Code Online (Sandbox Code Playgroud)