https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80187

            Bug ID: 80187
           Summary: C++ variant should be trivially copy constructible if
                    possible
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ambrop7 at gmail dot com
  Target Milestone: ---

Presumably because std::variant<int,char> is not trivially copy constructible,
the ABI will be unable to pass it by value in a register in a call to a
non-inlined function taking the variant by value, but will have to pass it by
reference.

Test case:

#include <variant>
struct TaggedUnion { union { int i; char c; }; int tag; };
void f1(std::variant<int,char>);
void f2(TaggedUnion);
int main ()
{
    f1(42);
    f2({42, 0});
    return 0;
}

Result on Linux x86-64: f1 is called passing the argument by address, f2 is
called passing by register.

See the standard proposal:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0602r0.html

however I think this can be fixed in gcc regardless of when/whether the
proposal is accepted.

Reply via email to