public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vincenzo.innocente at cern dot ch" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/109281] New: use std::optional results in suboptimal code
Date: Sat, 25 Mar 2023 11:55:21 +0000	[thread overview]
Message-ID: <bug-109281-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 109281
           Summary: use std::optional results in suboptimal code
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincenzo.innocente at cern dot ch
  Target Milestone: ---

In the following (almost real) code gcc emits suboptimal code if std::optional
is used w/r/t home made one and clang

see https://godbolt.org/z/Pba51Ye7Y


-----

code


#include <optional>

// #define USE_OPTIONAL

#ifdef USE_OPTIONAL
struct SubRingCrossings {
  SubRingCrossings(int ci, int ni, float nd) : closestIndex(ci), nextIndex(ni),
nextDistance(nd) {}

  int closestIndex;
  int nextIndex;
  float nextDistance;
};
#else
struct SubRingCrossings {
  SubRingCrossings() : valid(false) {}
  SubRingCrossings(int ci, int ni, float nd) : valid(true), closestIndex(ci),
nextIndex(ni), nextDistance(nd) {}

  bool valid;
  int closestIndex;
  int nextIndex;
  float nextDistance;
};
#endif

bool condition();

#ifdef USE_OPTIONAL
std::optional<SubRingCrossings> foo() {
    if (condition()) {
        return std::nullopt;
    }
    return SubRingCrossings(1, 2, 3.14);
}
#else
SubRingCrossings foo() {
    if (condition()) {
        return SubRingCrossings();
    }
    return SubRingCrossings(1, 2, 3.14);
}
#endif

int bar() {
    auto tmp = foo();
#ifdef USE_OPTIONAL
    if (tmp) {
        return tmp->closestIndex;
#else
    if (tmp.valid) {
        return tmp.closestIndex;
#endif
    } else {
        return 0;
    }
}

             reply	other threads:[~2023-03-25 11:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-25 11:55 vincenzo.innocente at cern dot ch [this message]
2023-03-25 18:48 ` [Bug tree-optimization/109281] " pinskia at gcc dot gnu.org
2023-04-02 17:03 ` pinskia at gcc dot gnu.org
2023-07-12 21:35 ` pinskia at gcc dot gnu.org
2023-07-12 21:35 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-109281-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).