vendredi 22 décembre 2017

How to construct a unique_ptr of a structure that contains an union?

I try to construct a std::unique_ptr of a structure that contains an enum and and union with two elements that are structures as well.

struct button_t
{
  std::uint8_t id;
  std::uint16_t status;
};

struct led_t
{
  std::uint8_t id;
  std::uint16_t status;
};

struct message_t
{
  enum class id_e : std::uint8_t { button_e, led_e } id;
  union
  {
    button_t button;
    led_t led;
  };

  message_t(message_t::id_e id, const button_t& button) : id(id), button(button) {}
  message_t(message_t::id_e id, const led_t& led) : id(id), led(led) {}
};

I am constructing a std::unique_ptr using std::make_unique as follow:

button_t button{56, 78};
typedef std::unique_ptr<message_t> p_msg_t;
auto msg = std::make_unique<p_msg_t>(message_t::id_e::button_event, button);

Well, I've got a compilator error:

error: no matching function for call to 'std::unique_ptr<message_t>::unique_ptr(message_t::id_e, button_t&)'

I don't see where the problem could be ?

Aucun commentaire:

Enregistrer un commentaire