0

I want to create a random number generation util for C++ and I want to create a method SetSeed that would generate and set a seed from input variadic arguments. I have such methods:

void SetSeedDirectly(int seed);

template<typename... Args>
void SetSeed(Args... args);

And the SetSeed method must generate a seed from args and call the SetSeedDirectly method. But I don't know how to generate a seed. I guess that I should use std::hash to generate a hash(seed) from args. I know how to get a hash from one value, but not from multiple values. I've tried to use tuple:

template <typename... Args>
void SetSeed(Args... args) {
  std::tuple<Args...> t(args...);
  std::hash<std::tuple<Args...>> h;
  size_t seed = h(t);
  this->SetSeedDirectly(static_cast<uint32_t>(seed));
}

But for some reason it doesn't work and compiler outputs errors like this:

'std::hash<std::tuple<int,std::string>>::hash(void)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Conditionally_enabled_hash<_Kty,false>::_Conditionally_enabled_hash(void)'

Hope for your help! Thanks in advance!

zenno2
  • 425
  • 2
  • 7
  • Create a [mcve] – eerorika Jul 31 '21 at 16:48
  • Any particular reason [](https://en.cppreference.com/w/cpp/header/random) ? – Richard Critten Jul 31 '21 at 16:54
  • 1
    There is no such constructor for `std::hash>` which is why the code fails. Look [here](https://en.cppreference.com/w/cpp/utility/hash) for all possible template arguments that `std::hash` can take. – Ruks Jul 31 '21 at 17:18
  • Consider `boost::hash_combine` - the implementation's only a few lines and can be found [here](https://stackoverflow.com/questions/35985960/c-why-is-boosthash-combine-the-best-way-to-combine-hash-values) along with some commentary on quality. It combines the hashes of multiple objects into one overall hash. – Tony Delroy Jul 31 '21 at 18:16
  • @TonyDelroy, thanks, it's good choice for me – zenno2 Aug 01 '21 at 03:38

0 Answers0