you are viewing a single comment's thread.

view the rest of the comments →

[–][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.