0

Let's suppose we have the following variadic template function:

template <int... Ints> 
void foo() {
  Foo<Ints...> f;
  // do something with f.
}

Note that foo need a sequence of integers at compile time, in order to build Foo class.

We can simply invoke it:

foo<1, 2, ,3 , 4>();

Is there any method to "mitigate" this function call? For example, to have something like:

foo(1, 2, 3, 4);

Notes:

  • No C-style (Macro, or vargs).
  • foo needs compile time sequence of integers.
  • Any C++ standard is fine.
BiagioF
  • 9,368
  • 2
  • 26
  • 50
  • that's possible for _types_ but non-type parameter pack you have no way to deduce those. pretty much macro is only way here (va_args are run-time, not compile time) – Swift - Friday Pie May 26 '18 at 10:54
  • 2
    But... why? Also, a variadic macro could be a pretty clean solution if you _really_ have to have this notation. In GCC: `#define foo(...) _foo< __VA_ARGS__ >()`. However, really, I see no good point. – Frax May 26 '18 at 11:03
  • I don't understand why do you want "mitigate" the template value based function call; what's wrong with it? – max66 May 26 '18 at 12:56
  • https://stackoverflow.com/questions/25680461/variadic-template-pack-expansion AND http://en.cppreference.com/w/cpp/language/parameter_pack Should be `foo(Ints... args)` – James Poag May 26 '18 at 13:42
  • `constexpr` arrays, initializer lists and ctors are a thing. This smells of abusing templates, there isn't really enough context to know if this is even the approach you should be taking. – BlamKiwi May 26 '18 at 14:27
  • How about `foo(1_c, 2_c, 3_c, 4_c)` with `operator""_c` which creates `std::integral_constant` – Jarod42 May 26 '18 at 17:12
  • @Jarod42 - nice idea; IMHO, you should propose it as an answer. – max66 May 26 '18 at 19:56

0 Answers0