自动非型模板参数:clang中的偏见偏专长

Auto non-type template parameter: ambiguous partial specializations in Clang

本文关键字:偏见 clang 非型 参数      更新时间:2023-10-16

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> {};
                      ^
  1. 为什么这是模棱两可的?e如何匹配0?如果我用nullptr替换E{},Clang停止抱怨。这看起来像一个叮当声。GCC可以很好地编译它。

  2. 如果是错误,什么是解决方法?就我而言,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