Class 里面不能有自己类型的常量?

Class can't have constants of own type inside?

我的意思是,是否有可能以某种方式做这样的事情?

class Color {
public:
    static constexpr Color BLACK = {0, 0, 0};

    constexpr Color(int r, int g, int b) : r_(r), g_(g), b_(b) {}

private:
    int r_;
    int g_;
    int b_;
};

编译器抱怨 class Color 在定义 BLACK 常量时不完整。

您可以将定义移到外面:

class Color {
public:
    static const Color BLACK;

    constexpr Color(int r, int g, int b) : r_(r), g_(g), b_(b) {}

private:
    int r_;
    int g_;
    int b_;
};
constexpr Color Color::BLACK = {0, 0, 0};

Demo

或者,您可以将静态变量更改为函数调用:

class Color {
public:
    static constexpr Color BLACK() { return {0, 0, 0}; }

    constexpr Color(int r, int g, int b) : r_(r), g_(g), b_(b) {}

private:
    int r_;
    int g_;
    int b_;
};