[英]How to create a c++ enum from a C style #define and struct (or what's the right way to do this)?
I'm working with a C library ( Raylib ) that uses the following for color representation:我正在使用 C 库( Raylib ),它使用以下颜色表示:
#define RED { 230, 41, 55, 255 }
// Color type, RGBA (32bit)
typedef struct Color {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
} Color;
I want to define an enum of all the Color
objects that I will use in my palette in my C++
code.我想在
C++
代码中定义我将在调色板中使用的所有Color
对象的枚举。
But enum class
only allows integral kinds of values.但是
enum class
只允许整数类型的值。 What's the best way to have a fixed static set of values which are non-integral?拥有固定的 static 一组非整数值的最佳方法是什么?
One approach I have in mind is just declare static constexpr
values in a struct
.我想到的一种方法是在
struct
中声明static constexpr
值。 Is this the right approach?这是正确的方法吗?
struct Color {
constexpr static auto MYRED = RED;
constexpr static auto MYBLUE = BLUE;
constexpr static auto MYGREEN = GREEN;
};
namespace RayLib {
using Color = ::Color;
inline constexpr Color Red = RED;
inline constexpr Color Blue = BLUE;
inline constexpr Color Green = GREEN;
}
is how I would do it.我会怎么做。
You might also want:您可能还想要:
namespace MyApp {
inline constexpr std::array Palette = {
RayLib::Red,
RayLib::Blue,
RayLib::Green,
RayLib::Fuscia
};
}
where MyApp
is the namespace you are using for app-specific code (in this case, the palette you are using in your app).其中
MyApp
是您用于应用程序特定代码的命名空间(在本例中,是您在应用程序中使用的调色板)。 (Apologies if I didn't get the deduction syntax quite right above) (抱歉,如果我没有完全正确地得到上面的演绎语法)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.