扩展非类型形参包来定义带有非类型形参的内部类模板是否合法?
Is it legal to expand non-type parameter pack to define internal class template with non-type template parameters?
我能够生成的重现问题的最小代码:
template <int>
struct Tag { };
Tag<0> w;
template <int... Is>
struct Outer {
template <Tag<Is> &...>
struct Inner {
};
};
int main() {
Outer<0>::Inner<w> f;
}
g++(版本6.1.1 20160511)在编译代码时遇到以下错误:
pp.cc: In function ‘int main()’: pp.cc:14:21: internal compiler error: unexpected expression ‘Is’ of kind template_parm_index Outer<0>::Inner<w> f;
并产生冗长而无聊的堆栈跟踪。版本3.6.0中的clang++在编译代码时似乎没有任何问题。带有类型模板参数的相同代码在两个编译器中都可以很好地编译:
template <class>
struct Tag { };
Tag<int> w;
template <class... Ts>
struct Outer {
template <Tag<Ts> &...>
struct Inner {
};
};
int main() {
Outer<int>::Inner<w> f;
}
所以这是一个g++的bug还是我错过了一些重要的关于非类型可变模板参数扩展不适用于类模板参数扩展?
(没有答案,但可能有人感兴趣)
对于GCC可能相对简单的解决方法:
template <int>
struct Tag { };
Tag<0> w;
template <class... Ts>
struct OuterParent {
template <Ts&...>
struct Inner {
};
};
template <int... Is>
struct Outer:OuterParent<Tag<Is>...> {
};
int main() {
Outer<0>::Inner<w> f;
}
相关文章:
- 非类型引用形参/实参
- 如何确保迭代器模板形参与模板类的模板形参具有相同的数据类型
- 如何在编译时通过模板形参默认值的名称/指针获取函数的类型
- 模板类的不同返回类型取决于类的形参
- 指向函数的指针vs作为模板非类型形参的函数
- 具有不同值类型模板形参的模板类的数组或向量
- 为什么模板非类型形参指针和引用实参需要是全局的
- 可以使用MemberFunction类型作为模板形参吗?
- c++ 03:是否有一种方法可以使一个类型每次被包含在模板形参中时都会编译成不同的类型?
- 将非const引用使用auto-keyword声明的lambda作为实参传递给std::函数形参类型
- 为什么标准不允许在模板形参列表中初始化依赖于常量的类型
- 在模板演绎中保持函数指针形参的完整类型
- 使用传入的字符串形参来访问类型中的内容
- 不能在模板专门化定义中将一个类的成员类型定义用作模板形参
- 传递给构造函数的形参不是类型
- 使用函数返回值作为默认模板非类型形参
- 扩展非类型形参包来定义带有非类型形参的内部类模板是否合法?
- 函数指针的形参类型的模板实参演绎涉及未演绎的形参包
- 可以基于引用形参类型重载构造函数吗?
- 对具有相同形参类型但具有不同常量限定符的std::vector进行强制转换