小编Tob*_*nge的帖子

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 不会编译并显示错误“错误:非命名空间范围内的显式专业化”。代码是否不符合标准?如果是这样——为什么?

c++ c++20

3
推荐指数
1
解决办法
133
查看次数

标签 统计

c++ ×1

c++20 ×1