【发布时间】:2021-09-15 01:45:53
【问题描述】:
考虑以下示例:
template< class A, int B, class C>
struct Obj {
A a_obj;
static constexpr int b_value = B;
C c_obj;
};
template< template<class... > class ... Templates >
struct Foo;
template<>
struct Foo<Obj> {
Obj<void*, 5, float> obj;
};
int main() {
Foo<Obj> foo;
};
这对我来说失败了(g++ 7.2,-std=c++17 出现以下错误:
templ.cpp:16:15: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class ...> class ... Templates> struct Foo’
16 | struct Foo<Obj> {
| ^
templ.cpp:16:15: note: expected a template of type ‘template<class ...> class ... Templates’, got ‘template<class A, int B, class C> struct Obj’
templ.cpp: In function ‘int main()’:
templ.cpp:23:8: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class ...> class ... Templates> struct Foo’
23 | Foo<Obj> foo;
| ^
templ.cpp:23:8: note: expected a template of type ‘template<class ...> class ... Templates’, got ‘template<class A, int B, class C> struct Obj’
它似乎没有得到匹配,虽然它看起来不像认为 template<class ...> class ... Templates 是语法错误,因此参数-参数包正在解析
这种类型的声明(有一些类型,一个常量值,然后是更多类型,或者最后可能是常量值)是否无法与可变参数模板模板包语法匹配?或者我没有正确使用语法
编辑
我修改了版本以避免在声明中使用非类型,但问题仍然存在:
template< template <class, class> class B, class A, class C>
struct Obj {
A a_obj;
//static constexpr int b_value = B;
B< C, A > b_obj;
C c_obj;
};
template< template<typename... > typename ... Templates >
struct Foo;
template<class A, class V>
struct Bar {
A a_obj;
//static constexpr int value = V;
V obj;
};
template<>
struct Foo<Obj> {
Obj<Bar, void*, float> obj;
};
int main() {
Foo<Obj> foo;
};
所以看起来不仅仅是非类型和类型不能在可变参数包上匹配,而是类型和模板的任何混合
我还尝试添加一个额外的可变参数包:
template< template<typename... > typename ... Templates, class ... Types >
struct Foo;
然后我得到:
error: parameter pack ‘template<class ...> class ... Templates’ must be at the end of the template parameter list
12 | template< template<typename... > typename ... Templates, class ... Types >
【问题讨论】:
-
您无法将类型和非类型的混合与可变参数包匹配。你需要像
template< template<class,int,class > class ... Templates >这样的东西。 -
一个类模板也不能有两个参数包。
标签: c++ c++17 variadic-templates template-templates