C++20 或更新版本是否可以显式特化类成员模板函数?

Tob*_*nge 3 c++ c++20

我有一个枚举和一个描述结构,具有完全专门的模板方法,可将枚举值转换为字符串。该代码适用于 msvc,但我收到 clang 警告和 gcc 错误。我如何或在哪里可以知道代码是否符合最新的 C++ 标准?

#include <stdint.h>

enum class E : uint8_t
{
  E1,
  E2
};

struct E_Description
{
    template <E>
    static constexpr auto to_string () noexcept;

    template <>
    static constexpr auto to_string<E::E1> () noexcept
    {
        return "E::E1";
    }
    
    template <>
    static constexpr auto to_string<E::E2> () noexcept
    {
        return "E::E2";
    }
};

int main()
{
    auto str{ E_Description::to_string<E::E1>() };
}
Run Code Online (Sandbox Code Playgroud)

此代码在 msvc 中按预期工作。使用 clang 我收到警告“警告:显式专业化不能有存储类”,并且 gcc 不会编译并显示错误“错误:非命名空间范围内的显式专业化”。代码是否不符合标准?如果是这样——为什么?

use*_*522 5

staticClang 是正确的,显式专业化不允许使用存储类说明符,请参阅[temp.expl.spec]/2。应该将其删除。MSVC 由于违规而未提供诊断,这是错误的。

自CWG 727决议以来,GCC 不接受类范围内的专业化是错误的。他们尚未实施此缺陷报告,并已为其公开错误报告。当前状态下的 GCC 希望您在类范围之外声明显式特化。