【发布时间】:2019-10-01 05:00:44
【问题描述】:
Clang(7, 8, trunk) 拒绝以下代码
enum class E {};
inline static constexpr auto e = E{};
// inline static constexpr auto e = nullptr;
template<auto, int> class S;
template<int a, int b> class S<a, b> {};
template<int b> class S<e, b> {};
int main() {
S<0, 0> s;
}
出现错误:
error: ambiguous partial specializations of 'S<0, 0>' note: partial specialization matches [with a = 0, b = 0] template<int a, int b> class S<a, b> {}; ^ note: partial specialization matches [with b = 0] template<int b> class S<e, b> {}; ^
为什么会模棱两可?
e怎么能匹配0?如果我用nullptr替换E{},Clang 停止抱怨。这看起来像 Clang 的错误。 GCC 编译它就好了。-
如果是错误,有什么解决方法?在我的例子中,
auto参数可以是E(并且只有一个值E{})或int。那么:template<auto, int, typename> class S_impl; template<int a, int b> class S_impl<a, b, int> {}; template<int b> class S_impl<e, b, E> {}; template<auto a, int b> using S = S_impl<a, b, decltype(a)>;还有更简洁的方法吗?
【问题讨论】:
标签: c++ templates clang c++17 auto