std::variant sum types
std::visit process the values in a std::variant
Sum types are compound types that have a range of values that is the sum of the ranges of their parts.
std::variant as a "safer union"
std::visit process the values in a std::variant
Sum types are compound types that have a range of values that is the sum of the ranges of their parts.
std::variant as a "safer union"
using ItsOk = std::variant<std::vector<int>, double>; int main() { ItsOk io = std::vector{22, 44, 66}; //set the variant to vector, this constructs the internal vector io = 13.7; // reset to double - the internal vector is properly destroyed int i = std::get<std::vector<int>>(io)[2]; // There's no vector in the variant - throws an exception }
std::ostream& operator<< (std::ostream& os, MyVariant const& v) { std::visit([&os](auto const& e){ os << e; }, v); return os; }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.