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

            Bug ID: 113791
           Summary: Incorrect handling of lvalue to rvalue conversion in
                    ternary operator
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Mark_B53 at yahoo dot com
  Target Milestone: ---

#include <unordered_map>
#include <iostream>

template<template<class, class, class...> class Map, typename Key, typename T,
typename KeyP, typename... Args>
T getMapEntry(const Map<Key, T, Args...>& map, const KeyP& key, T&& defval) {
    static_assert(std::is_convertible_v<KeyP, Key>);
    auto i = map.find(key);
    return i != map.end() ? i->second : std::move(defval);
}

class Foo {
public:
    Foo() { std::cout << __PRETTY_FUNCTION__ << '\n'; }
    ~Foo() { std::cout << __PRETTY_FUNCTION__ << '\n'; }
    Foo(const Foo&) { std::cout << __PRETTY_FUNCTION__ << '\n'; }
    Foo& operator=(const Foo&) { std::cout << __PRETTY_FUNCTION__ << '\n';
return *this; }
    Foo(Foo&&) { std::cout << __PRETTY_FUNCTION__ << '\n'; }
    Foo& operator=(Foo&&) { std::cout << __PRETTY_FUNCTION__ << '\n'; return
*this; }
};

int main() {
    std::unordered_map<int, Foo> map;
    auto&& ret = getMapEntry(map, 123U, Foo{});
    return 0;
}

GCC outputs:
Foo::Foo()
Foo::Foo(Foo&&)
Foo::~Foo()
Foo::~Foo()

Correct output:
Foo::Foo()
Foo::Foo(const Foo &)
Foo::~Foo()
Foo::~Foo()

Posted on https://stackoverflow.com/questions/77948860/

Maybe related to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87605

Reply via email to