struct rgb_color {
constexpr rgb_color(std::uint8_t nr, std::uint8_t ng, std::uint8_t nb) :
r(nr), g(ng), b(nb) { }
std::uint8_t r; // red
std::uint8_t g; // green
std::uint8_t b; // blue
constexpr static rgb_color black = rgb_color(0, 0, 0);
constexpr static rgb_color white = rgb_color(255, 255, 255);
};
Die constexpr static
konstanten Definitionen nicht kompilieren:Struct ist nicht-wörtlicher Typ
constexpr variable cannot have non-literal type 'const rgb_color'
jedoch http://en.cppreference.com/w/cpp/concept/LiteralType nach, const rgb_color
sollte ein wörtlicher Typ sein, weil es nur wörtliche Typen als Datenelement hat (std::uint8_t
) und der constexpr
Konstruktor.
Warum kompiliert der Code nicht?
Auch ist es notwendig, die constexpr static
Mitglieder in einer .cc
Datei, wie
constexpr rgb_color rgb_color::black;
Funktioniert es, wenn Sie 'constexpr statische rgb_color schwarz (0, 0, 0);'? – Sean
Nein: http://coliru.stacked-crooked.com/a/f7915407bb464659 – tmlen
Der Link, den Sie geben, hat einen Vorschlag: "möglicherweise cv-qualifiziert (C++ 17)". Ihr Compiler spielt hier möglicherweise nach den C++ 14 Regeln. – MSalters