This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug tree-optimization/83389] std::tie generates sub-optimal code when used to compare POD fields
- From: "lucanus81 at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 12 Dec 2017 13:34:05 +0000
- Subject: [Bug tree-optimization/83389] std::tie generates sub-optimal code when used to compare POD fields
- Auto-submitted: auto-generated
- References: <bug-83389-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83389
--- Comment #4 from Luca Stoppa <lucanus81 at gmail dot com> ---
(In reply to Jonathan Wakely from comment #2)
> This isn't a libstdc++ bug. std::tie is doing exactly what it's meant to do,
> which is generate a tuple of references, so of course it's not the same as
> comparing the original objects (because in the general case the references
> aren't all bound to members of the same object).
I see. Thanks for your detailed answer. I agree that using std::tie as a
surrogate for operator==() probably isn't the first usecase that comes to mind.
Please, feel free to close this missed-optimization.
>
> Reduced:
>
> struct data { int x, y; char c1, c2; };
>
> struct tuple { const int &x, &y; const char &c1, &c2; };
>
> bool operator==(const tuple& l, const tuple& r) {
> return l.x == r.x && l.y == r.y && l.c1 == r.c1 && l.c2 == r.c2;
> }
>
> bool operator==(const data& l, const data& r) {
> return l.x == r.x && l.y == r.y && l.c1 == r.c1 && l.c2 == r.c2;
> }
>
> bool compare(const data& l, const data& r) {
> return tuple{l.x, l.y, l.c1, l.c2} == tuple{r.x, r.y, r.c1, r.c2};
> }
>
> int main()
> {
> data d1{1,2,'a','b'};
> data d2{1,2,'a','d'};
>
> return (compare(d1,d2) ? 0 : -1);
> }