`template <auto>` 和部分 class 模板专业化排序

`template <auto>` and partial class template specialization ordering

考虑:

#include <type_traits>

template <typename>
struct Tag {};

template <typename T>
auto tag = Tag<T>{};


template <typename...>
struct SelectorImpl;

// 1
template <auto... xs>
struct SelectorImpl<std::integral_constant<decltype(xs), xs>...>
{};

// 2
template <typename T, Tag<T>* tag, auto... xs>
struct SelectorImpl<std::integral_constant<decltype(tag), tag>,
                    std::integral_constant<decltype(xs), xs>...>
{};


template <auto... params>
struct Selector
: SelectorImpl<std::integral_constant<decltype(params), params>...>
{};


int main() {
    Selector<&tag<int>, 1, 2>{};
}

gcc 和 clang 都无法编译它,报告 SelectorImpl 的特化不明确。我相信专业化 #2 更专业。我错了吗?它与 是同一个问题吗?这是一个错误吗?

首先,我想确定 <char X> 优先于 <auto X>。所以我写了这个简单的测试:https://godbolt.org/g/Hy6NVB

template<auto...>
struct TestSingleImpl;

// 1
template<auto x>
struct TestSingleImpl<x> { static constexpr bool value = false; };

// 2
template<char x>
struct TestSingleImpl<x> { static constexpr bool value = true; };

static_assert(TestSingleImpl<1234>::value == false, "1");
static_assert(TestSingleImpl<'a'>::value == true, "2");

结果:是的,它有优先权(没有歧义)

然后我用可变参数应用了相同的例子:https://godbolt.org/g/7mWaeH

template <typename...>
struct Test;

// 1
template<auto v, auto... x>
struct Test<std::integral_constant<decltype(v), v>,
            std::integral_constant<decltype(x), x>...>
{
    static constexpr bool value = true;
};

// 2
template<char v, auto... x>
struct Test<std::integral_constant<char, v>,
            std::integral_constant<decltype(x), x>...>
{
    static constexpr bool value = false;
};

template <auto... params>
struct Selector : Test<std::integral_constant<decltype(params), params>...> {};

static_assert(Selector<1234, 1, 2>::value == true, "1"); // 1 - ok
static_assert(Selector<'a', 1, 2>::value == false, "2"); // 2 - AMBIGUITY

看起来 template<char v, auto... x> 没有优先于 template<auto... x>

这看起来像是 CLANG 和 GCC 中的一个巨大错误

如果我们删除 std::integral_constant,它会正常工作:https://godbolt.org/g/grRC8h

这证实了错误 :)

template <auto...>
struct Test;

// 1
template<auto v, auto... x>
struct Test<v, x...>
{
    static constexpr bool value = true;
};

// 2
template<char v, auto... x>
struct Test<v, x...>
{
    static constexpr bool value = false;
};

static_assert(Test<1234, 1, 2>::value == true, "1"); // 1 - ok
static_assert(Test<'a', 1, 2>::value == false, "2"); // 2 - ok