什么是部分专门化用于void类型的parmeter包参数的语法?

Vio*_*ffe 6 c++ templates template-specialization c++17

我找不到办法让这个工作.它甚至可能吗?我不明白为什么不会这样.

template <auto id, typename FirstField, typename... OtherFields>
struct FieldTypeById {
    using Type = int;
};

template <auto id>
struct FieldTypeById<id, void> {
    using Type = void;
};


int main()
{
   using t1 = FieldTypeById<0, int>::Type;
   using t2 = FieldTypeById<1>::Type;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/z/AggnDq

Sto*_*ica 8

你的例子中的问题不是专业化,没关系.问题是FieldTypeById<1>无法推断出类型FirstField.您可以通过简单地向主模板添加默认值来修改它:

template <auto id, typename FirstField = void, typename... OtherFields>
struct FieldTypeById {
    using Type = int;
};
Run Code Online (Sandbox Code Playgroud)

现在所有参数都是明确给出的,取自默认值或推导出来(作为空包).在知道所有参数之后,可以使用这些参数的特化.

现场观看

  • @VioletGiraffe - 当模板的参数与之匹配时,将实例化特化.但默认参数只能出现在主模板上,因为这是模式匹配开始的地方(通俗地解释). (2认同)