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)
你的例子中的问题不是专业化,没关系.问题是FieldTypeById<1>无法推断出类型FirstField.您可以通过简单地向主模板添加默认值来修改它:
template <auto id, typename FirstField = void, typename... OtherFields>
struct FieldTypeById {
using Type = int;
};
Run Code Online (Sandbox Code Playgroud)
现在所有参数都是明确给出的,取自默认值或推导出来(作为空包).在知道所有参数之后,可以使用这些参数的特化.
| 归档时间: |
|
| 查看次数: |
135 次 |
| 最近记录: |