0

And to infer T from x instead? Obviously I can declare a function to do this:

template <typename T>
size_t get_std_hash(const T& x) {
    return std::hash<T>{}(x);
}

The question is in case I've overlooked something, and this function or some variation of it is already in the standard.

Alexey Romanov
  • 160,869
  • 33
  • 291
  • 457
  • 2
    Are you asking if the standard already provides a function that does the same thing as your `get_std_hash`? AFAIK no. – churill Oct 22 '21 at 14:33
  • 2
    as an expression? `std::hash{}(x)`? – Caleth Oct 22 '21 at 14:37
  • @Caleth I thought of that, but `x` can be a non-trivial expression, and I want to avoid repeating it for the same reason I don't want to specify `T` in the first place. – Alexey Romanov Oct 22 '21 at 14:43
  • Could this be an XY problem? You might be missing the `Hasher` template type, which could be defined as `template>`. Then your problem goes away. And if it was just an example then no, the object must be instantiated before operator() can be invoked, and obviously `std::hash{}` can't deduce `T`. – rustyx Oct 22 '21 at 15:00

0 Answers0