public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/95200] New: user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type
@ 2020-05-19  4:45 jevgenijsz1 at verifone dot com
  2020-05-19  8:21 ` [Bug libstdc++/95200] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: jevgenijsz1 at verifone dot com @ 2020-05-19  4:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95200
           Summary: user-defined hash function is not copied correctly if
                    unordered_map is declared using an incomplete type
           Product: gcc
           Version: 9.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jevgenijsz1 at verifone dot com
  Target Milestone: ---

Created attachment 48558
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48558&action=edit
g++ -v output

The failing code requires at least 3 source files, I did not find a cleaner way
to reproduce the issue. I'm not 100% sure this is a bug in libstdc++ however I
couldn't find a reason for why this should not work as expected.

Code:
=====
map_obj.h
=====

#ifndef MAP_OBJ_H_
#define MAP_OBJ_H_

#include <unordered_map>

enum class EnumType;

typedef std::unordered_map<EnumType, bool> map;

class MapObj {
 public:
  map getEnumMap() const;
  map enum_map_;
};

#endif
=====

=====
map_obj.cpp
=====
#include "map_obj.h"

map MapObj::getEnumMap() const {
  return enum_map_;
}
=====

=====
main.cpp
=====
#include <functional>

enum class EnumType : int {
  VALUE0,
  VALUE1,
};

namespace std {
template <>
struct hash<EnumType> {
  size_t operator()(EnumType type) const { return
std::hash<int>()(static_cast<int>(type)); }
};
}

#include "map_obj.h"

int main() {
  auto event = MapObj();
  event.enum_map_[EnumType::VALUE0] = true;
  event.enum_map_[EnumType::VALUE1] = false;
  event.getEnumMap().at(EnumType::VALUE0);
  event.getEnumMap().at(EnumType::VALUE1);
  return 0;
}
=====

using: 
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)
libstdc++ 3.4.28

command line to build: g++ -save-temps -Wall -Wextra -o map_test main.cpp
map_obj.cpp

expected result: runnnig map_test returns 0

actual result: terminate called after throwing an instance of
'std::out_of_range'
  what():  _Map_base::at

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

* [Bug libstdc++/95200] user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type
  2020-05-19  4:45 [Bug libstdc++/95200] New: user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type jevgenijsz1 at verifone dot com
@ 2020-05-19  8:21 ` redi at gcc dot gnu.org
  2020-05-19  8:25 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-19  8:21 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to jevgenijsz1 from comment #0)
> however I couldn't find a reason for why this should not work as expected.

[res.on.functions] paragraph 2:

In particular, the effects are undefined in the following cases:
- [...]
- If an incomplete type ([basic.types]) is used as a template argument when
instantiating a template component or evaluating a concept, unless specifically
allowed for that component.

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

* [Bug libstdc++/95200] user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type
  2020-05-19  4:45 [Bug libstdc++/95200] New: user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type jevgenijsz1 at verifone dot com
  2020-05-19  8:21 ` [Bug libstdc++/95200] " redi at gcc dot gnu.org
@ 2020-05-19  8:25 ` redi at gcc dot gnu.org
  2020-05-19  8:29 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-19  8:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Although the actual cause of the behaviour you see is due to violating a
different requirement. The explicit specialization hash<EnumType> is not
declared in map_obj.h which means it gets implicitly instantiated using the
default definition of std::hash for enumeration types. An explicit
specialization needs to be declared before any use that would cause the
implicit instantiation to happen.

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

* [Bug libstdc++/95200] user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type
  2020-05-19  4:45 [Bug libstdc++/95200] New: user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type jevgenijsz1 at verifone dot com
  2020-05-19  8:21 ` [Bug libstdc++/95200] " redi at gcc dot gnu.org
  2020-05-19  8:25 ` redi at gcc dot gnu.org
@ 2020-05-19  8:29 ` redi at gcc dot gnu.org
  2020-05-19 14:37 ` jevgenijsz1 at verifone dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-19  8:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
That's [temp.expl.spec] paragraph 7:

If a template, a member template or a member of a class template is explicitly
specialized then that specialization shall be declared before the first use of
that specialization that would cause an implicit instantiation to take place,
in every translation unit in which such a use occurs; no diagnostic is
required.

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

* [Bug libstdc++/95200] user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type
  2020-05-19  4:45 [Bug libstdc++/95200] New: user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type jevgenijsz1 at verifone dot com
                   ` (2 preceding siblings ...)
  2020-05-19  8:29 ` redi at gcc dot gnu.org
@ 2020-05-19 14:37 ` jevgenijsz1 at verifone dot com
  2020-05-19 15:07 ` jevgenijsz1 at verifone dot com
  2020-05-19 15:45 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jevgenijsz1 at verifone dot com @ 2020-05-19 14:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from jevgenijsz1 at verifone dot com ---
Jonathan,

Thank you for looking into this. I guess what confused me was copy assignment
requirements in 26.2.7 that explicitly say hash function is copied and the hash
function is available at the time of copy

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

* [Bug libstdc++/95200] user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type
  2020-05-19  4:45 [Bug libstdc++/95200] New: user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type jevgenijsz1 at verifone dot com
                   ` (3 preceding siblings ...)
  2020-05-19 14:37 ` jevgenijsz1 at verifone dot com
@ 2020-05-19 15:07 ` jevgenijsz1 at verifone dot com
  2020-05-19 15:45 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jevgenijsz1 at verifone dot com @ 2020-05-19 15:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from jevgenijsz1 at verifone dot com ---
Moreover if I was to put a breakpoint in struct hash<EnumType> I can see that
it is being used 4 times in the code example posted: twice to hash the EnumType
on insertion and twice on access (however the second access attempt throws an
exception)

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

* [Bug libstdc++/95200] user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type
  2020-05-19  4:45 [Bug libstdc++/95200] New: user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type jevgenijsz1 at verifone dot com
                   ` (4 preceding siblings ...)
  2020-05-19 15:07 ` jevgenijsz1 at verifone dot com
@ 2020-05-19 15:45 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-19 15:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It's undefined behaviour so anything can happen.

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

end of thread, other threads:[~2020-05-19 15:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-19  4:45 [Bug libstdc++/95200] New: user-defined hash function is not copied correctly if unordered_map is declared using an incomplete type jevgenijsz1 at verifone dot com
2020-05-19  8:21 ` [Bug libstdc++/95200] " redi at gcc dot gnu.org
2020-05-19  8:25 ` redi at gcc dot gnu.org
2020-05-19  8:29 ` redi at gcc dot gnu.org
2020-05-19 14:37 ` jevgenijsz1 at verifone dot com
2020-05-19 15:07 ` jevgenijsz1 at verifone dot com
2020-05-19 15:45 ` redi 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).