Advanced C++
Q74 / 100

What is std::variant and when is it preferred over union?

Correct! Well done.

Incorrect.

The correct answer is B) std::variant is a type-safe discriminated union that tracks which type is active and calls constructors/destructors correctly

B

Correct Answer

std::variant is a type-safe discriminated union that tracks which type is active and calls constructors/destructors correctly

Explanation

C unions don't track the active member and don't call constructors for non-trivial types. std::variant is type-safe, runs constructors/destructors, and enables std::visit.

Progress
74/100