自动非类型模板参数:Clang中模棱两可的局部专业化

Evg*_*Evg 10 c++ templates clang auto c++17

铛(7,8,主干)拒绝以下代码

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;
}
Run Code Online (Sandbox Code Playgroud)

带有错误:

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> {};
                      ^
Run Code Online (Sandbox Code Playgroud)
  1. 为什么模棱两可?如何e搭配0?如果我替换E{}nullptr,则Clang停止抱怨。这看起来像是Clang的bug。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)>;
    
    Run Code Online (Sandbox Code Playgroud)

    还有更简洁的方法吗?

Clo*_*onk 1

Clang 的推导错误。它与此 bug类似,链接到此问题(与您在模板参数中使用 auto 不完全相同,这将阻止您使用 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
}
Run Code Online (Sandbox Code Playgroud)

任何强制推导类型模板参数的解决方案都将起作用,因此您建议的解决方案是完全有效的。恕我直言,当不需要提高可读性时,我会避免在模板参数中使用 auto :

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
Run Code Online (Sandbox Code Playgroud)