public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113791] New: Incorrect handling of lvalue to rvalue conversion in ternary operator
@ 2024-02-06 20:27 Mark_B53 at yahoo dot com
  2024-02-06 20:48 ` [Bug c++/113791] " mpolacek at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Mark_B53 at yahoo dot com @ 2024-02-06 20:27 UTC (permalink / raw)
  To: gcc-bugs

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

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

end of thread, other threads:[~2024-02-15 17:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-06 20:27 [Bug c++/113791] New: Incorrect handling of lvalue to rvalue conversion in ternary operator Mark_B53 at yahoo dot com
2024-02-06 20:48 ` [Bug c++/113791] " mpolacek at gcc dot gnu.org
2024-02-06 21:42 ` mpolacek at gcc dot gnu.org
2024-02-07  0:33 ` pinskia at gcc dot gnu.org
2024-02-07  0:35 ` pinskia at gcc dot gnu.org
2024-02-15 17:44 ` mpolacek at gcc dot gnu.org
2024-02-15 17:56 ` mpolacek 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).