Clang 和 GCC 在转换 C++17 中非类型模板参数的自动说明符方面存在分歧

Clang and GCC disagree in auto specifier for non-type template parameter in a casting C++17

我基本上有一个 class 依赖于非类型模板参数。我定义了一个转换,因此非类型模板参数 N 的对象可以转换为另一个 M。我有一个可以重现这种情况的最小示例:

template<auto Integral>
class Test{
    public:
        typedef decltype(Integral) value_type;
        static constexpr value_type N = Integral;

        constexpr Test (const value_type& x = 0);

        template<auto Integral2>
        constexpr explicit operator Test<Integral2>() const;
    private:
        value_type n;
};

template<auto Integral>
constexpr Test<Integral>::Test (const value_type& x){
    if (x < 0){
        n = N - (-x)%N;
    }
    else{
        n = x%N;
    }
}

template<auto Integral> template<auto Integral2>
constexpr Test<Integral>::operator Test<Integral2>() const{
    return Test<Integral2>(n%(Test<Integral2>::N));
}

我在 Ubuntu 16.04 LTS 中使用 GCC 7.2.0 和 Clang 5.0.0 进行编译,标志为 -O2 -std=c++17.

问题是我一直在使用 g++ 进行编译,并且一切都按预期工作,但后来我尝试了 clang++ 来检查一切是否仍在编译正常。

令我惊讶的是,事实并非如此,因为 clang++ 抱怨 g++ 没有的某些部分。您可以在 Compiler Explorer.

中查看差异视图

clang++ 产生的错误消息之一是:

error: out-of-line definition of 'operator Test<Integral2>' does not match any declaration in 'Test<Integral>'

这个让我觉得 clang++ 没有找到转换的 "right" 声明,但我不知道。

注意:第一个错误仅在将声明与定义分开时出现。否则,似乎是正确的。

这是 clang++ 产生的第二个错误:

error: a non-type template parameter cannot have type 'auto'

但是这个错误更让我吃惊,因为这个错误告诉了一些在 C++17 中应该有效的东西。可能我在这里遗漏了一些东西,因为这对我来说没有意义。

请记住,第二个错误仅出现在这种转换的情况下。实际代码中的任何地方都会给出错误(即使有更多 auto 非类型模板参数)。

此时,我有一些疑问:

这是一个 clang 错误。这是一个简短的复制:

template <int A>
struct X {
    template <auto B>
    X<B> foo();
};

template <int A>
template <auto B>
X<B> X<A>::foo() {
    return {};
}

如果auto Bint B代替,clang接受。 gcc 按原样接受它。对于 clang,这只是嵌套 template 声明的问题。 auto 作为占位符模板非类型参数,没有任何东西可以防止它被用来定义一些不合时宜的东西。

在提交一个新的 clang bug 时,我发现 35655,复制更短:

template<typename>
struct S {
    template<auto n>
    static void f() {
        +n;
    }
};

失败:

source.cpp:5:3: error: invalid argument type 'auto' to unary expression
                +n;