自动非型模板参数:clang中的偏见偏专长
Auto non-type template parameter: ambiguous partial specializations in Clang
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停止抱怨。这看起来像一个叮当声。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)>;
有更简洁的方式吗?
clang的推论错误。它类似于此错误,链接到此问题(与模板参数中使用自动的情况并不完全相同,这将阻止您使用STDC 14编译(。
一个有趣的情况是,如果它是一个完整的专业化,那不是这种情况。仅针对部分专业化:
#include <iostream>
enum class E {};
inline static constexpr auto e = E{};
template <auto a, int b>
class FOO;
template <int a, int b > class FOO<a, b> {};
template <int b> class FOO<e, b> {};
template <auto a, int b>
class BAR;
template <int a, int b > class BAR<a, b> {};
template <> class BAR<e, 0> {};
template <auto a>
class BAZ;
template <int a> class BAZ<a> {};
template <> class BAZ<e> {};
int main() {
// FOO <0, 0> foo; // <= Not Ok
BAR<0, 0> bar; // <= Ok
BAZ<0> baz; // <= Ok
}
迫使扣除类型模板参数的任何解决方案都将起作用,因此您建议的解决方案是完全有效的。恕我直言,如果不需要提高可读性,我会避免在模板参数中使用自动:
template <typename T, T value, int> class S_impl; // <= this auto is not necessary
template <int a, int b> class S_impl<int, a, b> {};
template <int b> class S_impl<E, e, b> {};
// Either define S to use S<0,0> or directly use S_impl<int, 0, 0>
template <auto a, int b> using S = S_impl<decltype(a), a, b> // <= this auto is necessary
相关文章:
- 奇怪的结构&GCC&clang(void*返回类型)
- 数据成员SFINAE的C++17测试:gcc vs clang
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- Clang bug?使用指针作为模板参数
- clang整洁10忽略了我的NOLINT命令
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在clang++预处理器中确定gcc工具链版本
- 为什么 Clang 不允许"and"作为函数名称?
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- clang格式:宏的缩进
- CLANG 编译器 说:变量"PTR"可能未初始化
- clang格式:禁用排序包含
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 循环展开 - G++ 与 Clang++
- 如何使用Clang/GCC在Mac上为C/C++设置VSCode
- 自动非型模板参数:clang中的偏见偏专长
- 声明一个在clang中的非偏见的变量