0

I have following function:

template <typename T, T... Ts> T random_value(default_random_engine &rnd)
{
    static constexpr T values [] { Ts... };
    static uniform_int_distribution<size_t> distribution(0, sizeof...(Ts) - 1);
    return values[distribution(rnd)];
}

That is called just so:

MyEnum val = random_value<MyEnum, MyEnum::val1, MyEnum::val2, MyEnum::val3>(rnd);

Is it possible to re-define it in such a way to I don't need to specify T explicitly? So that it is called just so:

MyEnum val = random_value<MyEnum::val1, MyEnum::val2, MyEnum::val3>(rnd);
Ghostrider
  • 7,545
  • 7
  • 30
  • 44
  • you could use a macro with decltype obviously, but otherwise i think bryan is right – Chris Beck Nov 03 '16 at 23:02
  • 2
    Sadly you cannot do it elegantly (or at all) in C++ (for the moment). Like Chris said the only way around this is with a macro: `#define RANDOM_VALUE(x,...)random_value()` – David G Nov 03 '16 at 23:04

1 Answers1

5

Not at this time, but the new auto template parameter feature being added in C++17 will help with this. See Advantages of auto in template parameters in C++17

Community
  • 1
  • 1
Brian Bi
  • 111,498
  • 10
  • 176
  • 312