【发布时间】:2016-06-11 19:06:26
【问题描述】:
考虑代码:
#include <iostream>
template <class... Ts>
struct outer {
template <class... ITs>
struct inner {
static constexpr bool value = false;
};
template <class... ITs>
struct inner<Ts..., ITs...> {
static constexpr bool value = true;
};
};
int main() {
std::cout << outer<int, float, double>::inner<int, float, double, int>::value << std::endl;
}
代码用 clang++ 编译,但不能用 g++ 编译,它会产生错误:
temp3.cc:11:11:错误:参数包参数“Ts ...”必须位于 模板参数列表的结尾
struct inner<Ts..., ITs...> { ^
正如我已经建立的here 内部类的部分专业化应该是合法的。
编辑: 为了完整起见,值得补充的是,上述代码的 clang 警告他在推导 IT 参数时可能会遇到问题,但没有任何问题......
【问题讨论】:
-
我并不确切地知道规则,但是当我遇到依赖类型的错误时,在它之前添加
typename或template有时会有所帮助。试试struct inner<typename Ts...。 -
添加
typename后我得到temp3.cc:11:39: error: template argument 1 is invalid -
需要注意的是,请求的场景仍然可以通过一些额外的模板元编程来实现...coliru.stacked-crooked.com/a/0c6c643c8ff5809e(是的,我知道这不是问题,但实现它的挑战是不可避免的...)。
标签: c++ templates c++11 variadic-templates template-specialization