public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109281] New: use std::optional results in suboptimal code
@ 2023-03-25 11:55 vincenzo.innocente at cern dot ch
  2023-03-25 18:48 ` [Bug tree-optimization/109281] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: vincenzo.innocente at cern dot ch @ 2023-03-25 11:55 UTC (permalink / raw)
  To: gcc-bugs

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;
    }
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-07-12 21:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-25 11:55 [Bug c++/109281] New: use std::optional results in suboptimal code vincenzo.innocente at cern dot ch
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

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).