SFINAE 模板复杂类型的静态成员定义 class

Static member definition of complex type of a template class with SFINAE

声明和定义模板复杂类型的静态常量成员是相当标准的方法class。但是,我会使用 SFINAE patter in my class. And that leads to this error: template definition of non-template 'const std::array<unsigned int, 2ul> Message<Self>::kData'. Removing SFINAE 解决错误。

代码如下:

#include <iostream>
#include <type_traits>
#include <array>

using namespace std;

template <class Self, typename = std::enable_if_t<std::is_class<Self>::value>>
class Message
{
  public:
    bool Verify() const { return static_cast<const Self *>(this)->Verify(); }
    
    static const std::array<uint32_t, 2> kData;
};

class Command : public Message<Command>
{
  public:
    bool Verify() {
        std::cout << "Command::Verify";
        return true;
    }
};

template <class Self, typename = std::enable_if_t<std::is_class<Self>::value>>
const std::array<uint32_t, 2> Message<Self>::kData = { 12, 13 };

int main()
{
    Command cmd;
    cout << "Data: " << cmd.kData[0] << endl;

    return 0;
}

实时代码是 here。 仍然使用 SFINAE 并正确定义静态常量的正确语法是什么?

What is the proper syntax to still use SFINAE and properly define the static constant?

简单地使用泛型类型T

// ............................V
template <class Self, typename T>
const std::array<uint32_t, 2> Message<Self, T>::kData = { 12, 13 };
// .......................................^^^

模板默认 types/values 未在外部定义中使用。