C ++ 17中的部分类模板参数推导

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

是否可以以某种方式进行部分扣除?如果不可能直接解决,有什么好的解决方法?

Nat*_*ica 5

CTAD(类模板参数推导)当前是一个全有或全无过程。您可以不指定任何内容并允许编译器推断所有参数,或者指定所有参数将编译器带出循环。

有一篇论文(P1021R0)要求更多,但尚未被接受。有一篇论文要求部分专业化,但在修订后已被删除。 最新版本仍包含使用别名时具有CTAD功能的建议。


Per @ Barry对Alias模板(P1814)和聚合(P1816)的支持已添加到C ++ 20的工作草案中。不添加对部分CTAD或具有继承的构造函数的CTAD的支持。


Jar*_*d42 5

您可以添加扣除指南如下:

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)