【发布时间】:2016-11-18 19:17:10
【问题描述】:
以下代码使用枚举成员m 作为常量表达式,即作为模板参数。该代码在 gcc 下编译,但不在 clang (live demo) 下编译。 Clang 说“错误:非类型模板参数不是常量表达式”。
问题可以通过将// 1 换成A<tst<p>::m> a 来解决。因此,我的问题不是如何解决这个问题,而是哪个编译器是正确的。
template<size_t n> struct A{};
template<size_t n>
struct tst
{ enum : size_t { m= n % 15 };
template<size_t p>
void
call( tst<p> const &t2 ) {
A<t2.m> a; // 1
}
};
【问题讨论】:
-
This references says "当一个无作用域的枚举是类成员时,可以使用类成员访问运算符
.和->访问它的枚举数"。但是它没有提到任何关于常量表达式的内容。 -
@JoachimPileborg 标准
§ 7.2.2确实说The identifiers in an enumerator-list are declared as constants, and can appear wherever constants are required.