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: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected], [email protected],
                    [email protected]

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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to