Почему сама функция хэша кортежа, то есть оператор (), должен быть const?C++

Программы на C++. Форум разработчиков
Anonymous
Почему сама функция хэша кортежа, то есть оператор (), должен быть const?

Сообщение Anonymous »

Я пытаюсь создать std :: unomordomed_set чей ключ типа std :: tuple .
Приведенный ниже код, скомпилированный MSVC, дает мне:

Error C3848 expression having type 'const _Hasher' would lose some const-volatile qualifiers
in order to call 'size_t TupleHash::operator ()(const std::tuple &)'
< /code>

Код: Выделить всё

struct TupleHash
{
// Uncomment the const keyword below resolves the error
template
std::size_t operator()(const std::tuple &t) // const
{
return std::hash{}(std::get(t)) ^ (std::hash{}(std::get(t))  

Подробнее здесь: [url]https://stackoverflow.com/questions/79673004/why-does-the-tuple-hash-function-itself-i-e-operator-need-to-be-const[/url]

Вернуться в «C++»