mardi 26 avril 2016

Struct is non-literal type

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);
};

The constexpr static constant definitions fail to compile:

constexpr variable cannot have non-literal type 'const rgb_color'

However according to http://ift.tt/1COqokG, const rgb_color should be a literal type, because it has only literal types as data members (std::uint8_t), and the constexpr constructor.

Why does the code not compile?

Also, is it necessary to define the constexpr static members in a .cc file, like

constexpr rgb_color rgb_color::black;

Aucun commentaire:

Enregistrer un commentaire