This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/77495] New: optional<T> assignment from {} acts weirdly


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

            Bug ID: 77495
           Summary: optional<T> assignment from {} acts weirdly
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eric at efcs dot ca
  Target Milestone: ---

Assignment to optional<T> from {} acts differently depending on if 'T' can be
constructed from {} without a used defined conversion. If so then then
operator=(U&&) is selected. Otherwise operator=(optional&&) is selected after
converting {} to optional<T>.

Reproducer:

#include <optional>
#include <cassert>

using namespace std;

struct T { T() = default; };

int main() {
    optional<int> oi(in_place);
    oi = {}; // converts {} to int because conversion isn't user defined.
    assert(oi.has_value() == true);
    optional<T> ot(in_place);
    ot = {}; // converts {} to optional<T> then calls operator=(optional&&)
    assert(ot.has_value() == false);
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]