[cfe-dev] std::pair not trivially copyable?

David Blaikie via cfe-dev cfe-dev at lists.llvm.org
Tue Aug 25 14:37:23 PDT 2020


On Tue, Aug 25, 2020 at 5:52 AM Robinson, Paul via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> We had someone trip over a case that reduces to this:
>
> F:\scratch>type tc6347.cpp
> #include <utility>
> std::pair<unsigned, bool> data[10];
> void bar() {
>   for (const auto item : data)
>     (void)item.first;
> }
>
> F:\scratch>%wo%\clang -c -Wall tc6347.cpp
> tc6347.cpp:4:19: warning: loop variable 'item' of type 'const
> std::pair<unsigned
>       int, bool>' creates a copy from type 'const std::pair<unsigned int,
> bool>'
>       [-Wrange-loop-construct]
>   for (const auto item : data)
>                   ^
> tc6347.cpp:4:8: note: use reference type 'const std::pair<unsigned int,
> bool> &'
>       to prevent copying
>   for (const auto item : data)
>        ^~~~~~~~~~~~~~~~~
>                   &
> 1 warning generated.
>
> F:\scratch>
>
>
> This used to compile cleanly.  It turns out that the diagnostic
> used to be suppressed for POD types, and now it is suppressed for
> trivially-copyable types.
>
> So... a std::pair of two scalars is not trivially copyable?
>

Looks like it isn't trivially copy /assignable/, but it is trivially copy
constructible in both libc++ and libstdc++: https://godbolt.org/z/Md9f78

So, one question (why is std::pair of two trivial types not trivially
assignable?) and /probably/ one bug (why does the warning flag on a
trivially copy constructible type).

>From what I can tell, reading this (
https://stackoverflow.com/questions/58283694/why-is-pair-of-const-trivially-copyable-but-pair-is-not
)
and the C++17 spec, it just doesn't specify the copy and move assignment
operators as defaulted, or say anything about their triviality, so they
aren't trivial even for trivial types. Perhaps an oversight when thinking
about the other complexities of when they shuold be deleted.

I'd have thought it would be, but then C++ is full of surprises.
>
> Thanks,
> --paulr
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200825/6d74f46a/attachment.html>


More information about the cfe-dev mailing list