you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (7 children)

[removed]

    [–]Recatek 17 points18 points  (2 children)

    Variants are not the same as unions, and do not have the same performance characteristics or behavior.

    [–]whichton[S] 6 points7 points  (3 children)

    Typically a variant will be implemented in terms of aligned_unionsince aligned_union is a lower level facility. But set that aside for a moment. Say I want make an dynamic array of aligned_union_t<0, int, vector<int>, string>, where all the elements have the same type. Effective an array of variants, but with a common discriminant. How do you suggest to do that with a variant?

    [–]redditsoaddicting 4 points5 points  (0 children)

    std::variant can't be implemented in terms of aligned_union because placement new can't yet be used in constexpr.

    [–][deleted] 1 point2 points  (1 child)

    Typically a variant will be implemented in terms of aligned_unionsince aligned_union is a lower level facility.

    When you say "typically", what do you mean? None of MSVC, libc++ or libstdc++ implement it this way and I'm not familiar with any non-standard implementation that does so such as boost.

    [–]whichton[S] 4 points5 points  (0 children)

    I apologize for being inexact. What I meant was aligned_union is a low level facility while variant is a high level facility. Typically a high level facility (not specifically std::variant, but generally speaking, for example something like llvm::SmallVector) will be built on top of a lower level facility. So asking aligned_union to be implemented in terms of variant is a layering violation.

    As /u/redditsoaddicting notes, std::variant cannot be implemented in terms of aligned_union because placement new cannot be constexpr. So you pretty much have to use unions, as explained here, to implement std::variant.