mardi 31 octobre 2017

Access fields in a named union

I want to have a named union in the following struct so that I can memcpy it without knowing what field is "active".

struct Literal {
  enum class Type : size_t {
    INT = 1,
    LONG,
    FLOAT,
    DOUBLE
  } type;

  union {
    int li;
    long ll;
    float lf;
    double ld;
  } v;

  constexpr Literal(int li): type{Type::INT}, v.li{li} {}
  constexpr Literal(long ll): type{Type::LONG}, v.ll{ll} {}
  constexpr Literal(float lf): type{Type::FLOAT}, v.lf{lf} {}
  constexpr Literal(double ld): type{Type::DOUBLE}, v.ld{ld} {}
};

How can I initialize the fields in the constructors? Neither v.li{li} nor li{li} are working.

I also tried v{li} but it work only for the first constructor because it cast the 3 others to int.

Aucun commentaire:

Enregistrer un commentaire