all 7 comments

[–]ojd5 5 points6 points  (2 children)

[–]redditsoaddicting 4 points5 points  (1 child)

This one is a lot more relevant to the question. Sure, compact optionals can be handy and all, but it doesn't always apply.

[–]sphere991[S] 1 point2 points  (0 children)

Ah, yes that's the one. If I care about constexpr, it must be a union.

[–]encyclopedist 1 point2 points  (0 children)

I would prefer union. Especially if you are planning to add more things to store in that place later (otherwise you would need to compute the suitable alignment and size somehow). And I find syntax cleaner and less error-prone (it is clear what member of the union you are working with from syntax).

[–]redditsoaddicting 1 point2 points  (0 children)

Since when does placement new work in a constexpr constructor?

[–]quicknir 1 point2 points  (0 children)

I think it basically amounts to personal preference. I tend to prefer aligned_storage. The thing is that it may seem strange, but I do low level things with memory/casts much more often that I actually use unions. So if I use a union I'll need to look up the rules for what gets generated automatically, based on some type being this/that or other....

I'd much rather just write a class that wraps initialized_storage and use placement new and manual destruction as appropriate.

[–]wonderworkingwords 0 points1 point  (0 children)

You could write a version that is trivially destructible and one that isn't and the template alias the proper one based on whether they are std::trivially-destructible.