Clang和GCC在铸造C++ 17中对非类型模板参数的auto说明符不同意

Fra*_*ido 18 c++ g++ language-lawyer clang++ c++17

我基本上有一个依赖于非类型模板参数的类.我定义了一个转换,所以非类型模板参数的对象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));
}
Run Code Online (Sandbox Code Playgroud)

我正在使用GCC 7.2.0和Clang 5.0.0在Ubuntu 16.04 LTS中使用标志进行编译-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>'
Run Code Online (Sandbox Code Playgroud)

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

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

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

error: a non-type template parameter cannot have type 'auto'
Run Code Online (Sandbox Code Playgroud)

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

请注意,此转换仅出现此第二个错误.实际代码中的任何位置都会产生错误(即使有更多auto非类型模板参数).

在这一点上,我有一些问题:

  • 在clang编译器的情况下产生错误的原因是什么?
  • 根据标准,哪一个是正确的?

Bar*_*rry 11

这是一个铿锵的错误.这是一个简短的复制:

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

template <int A>
template <auto B>
X<B> X<A>::foo() {
    return {};
}
Run Code Online (Sandbox Code Playgroud)

如果auto B被替换int B,clang接受它.gcc按原样接受它.对于clang,这只是嵌套template声明的问题.auto作为占位符模板非类型参数,没有任何内容可以防止它被用于定义异类内容.

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

template<typename>
struct S {
    template<auto n>
    static void f() {
        +n;
    }
};
Run Code Online (Sandbox Code Playgroud)

失败的是:

source.cpp:5:3: error: invalid argument type 'auto' to unary expression
                +n;
Run Code Online (Sandbox Code Playgroud)