<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - unordered_map accepts non const hasher functor"
   href="https://bugs.llvm.org/show_bug.cgi?id=49194">49194</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>unordered_map accepts non const hasher functor
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++11
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>anton.veselskyi@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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>;
```</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>