https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85771
Bug ID: 85771 Summary: `std::variant<...>` insanely slow to compile compared to `union` (256 types) Product: gcc Version: 8.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: vittorio.romeo at outlook dot com Target Milestone: --- Consider the following set of types: struct Type0 { int x; }; struct Type1 { int x; }; struct Type2 { int x; }; // ... struct Type253 { int x; }; struct Type254 { int x; }; struct Type255 { int x; }; Now imagine creating both a `std::variant` and a `union` out of them, then compiling some code that prints out the active alternative with `-O3`: union MyVariant { Type0 type0; /* ... */; Type255 type255; }; std::variant<Type0, /* ... */, Type255> v; These are the *compilation time* results I get: UNION real 0m0.182s user 0m0.104s sys 0m0.068s VARIANT real 0m2.259s user 0m1.960s sys 0m0.280s I can get similar results on widely different machines (40 core company development box, my laptop, and an online compiler). Fully-reproducible live demo on coliru: http://coliru.stacked-crooked.com/a/65d2ec5f4d7eb1ca I think that this kind of difference is unacceptable, and `std::variant` types with a lot of alternatives can occur in realistic use cases. If we want to promote safety and abstraction over C-style code, the compilation time costs must be sane.