[llvm-bugs] [Bug 49194] New: unordered_map accepts non const hasher functor

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Feb 15 09:31:28 PST 2021


https://bugs.llvm.org/show_bug.cgi?id=49194

            Bug ID: 49194
           Summary: unordered_map accepts non const hasher functor
           Product: clang
           Version: trunk
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: anton.veselskyi at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

If I want to create a unordered_map with a custom hasher, I need to provide a
functor that accepts the Key as a 3rd template parameter.

On all other compilers functors operator() should accept a Key as a const
parameter, if it is not const code will not compile.

clang ignores this error.


INVALID CODE THAT SHOULD NOT COMPILED(allowing hasher to change Key object):
```
struct pizza_hasher
{
    size_t operator()(std::set<std::string> &s) const
    {
        std::hash<std::string> string_hasher;
        size_t res = 23;

        for(const string &str : s)
            res ^= string_hasher(str);

        return res;
    }
};


using Pizzas = std::unordered_map<std::set<std::string>, std::vector<int>,
pizza_hasher>;
```

reproduced on:
Apple clang version 12.0.0 (clang-1200.0.32.29)

EXPECTED RESULT:
compilation error

ACTUAL RESULT:
compilation OK



valid code as a reference:
```
struct pizza_hasher
{
    size_t operator()(const std::set<std::string> &s) const
    {
        std::hash<std::string> string_hasher;
        size_t res = 23;

        for(const string &str : s)
            res ^= string_hasher(str);

        return res;
    }
};


using Pizzas = std::unordered_map<std::set<std::string>, std::vector<int>,
pizza_hasher>;
```

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210215/978b8b8b/attachment.html>


More information about the llvm-bugs mailing list