#include <iostream>
#include <string>
#include <cstdint>
using namespace std;
enum class Kind : uint8_t { Id, Value };
struct Packet {
Kind kind;
union {
uint16_t id;
int32_t value;
};
};
int main() {
string type;
int num;
cin >> type >> num;
Packet p;
if (type == "Id") {
p.kind = Kind::Id;
p.id = (uint16_t)num;
} else if (type == "Value") {
p.kind = Kind::Value;
p.value = num;
}
if (p.kind == Kind::Id) {
cout << "ID=" << p.id;
} else if (p.kind == Kind::Value) {
cout << "VALUE=" << p.value;
}
return 0;
}
Solution Details
👉 In simple words: The tag (Kind) acts like a label saying “right now this union holds an ID” or “right now it holds a Value.”
Significance for Embedded Developers
Input
Id 42
Expected Output
ID=42