Preamble: I've looked at constexpr initializing static member using static function, but (thanks to Oleg Bogdanov's answer) I'm not trying to initialize a static.
I'd like to know how to make the following work:
typedef uint32_t color_t; // represent color as 00rrggbb
class Color {
static color_t makeColor(const uint8_t r,
const uint8_t g,
const uint8_t b) {
return (((color_t)r << 16) | ((color_t)g << 8) | (color_t)b);
}
static const color_t kRed = makeColor(255, 0, 0);
}
As I see it, the compiler needs to be told that it can evaluate makeColor()
at compile time, so I think this is a job for constexpr
. Despite my best efforts at sprinkling around constexpr
and const
, I still get
error: field initializer is not constant
What am I missing?
P.S.: I can certainly accomplish what I want with #define:
#define makeColor(r, g, b) (((color_t)(r) << 16) | ((color_t)(g) << 8) | (color_t)(b))
... but that seems so 20th century!