Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a sequence of type T at compile time

I have the following problem:

template< typename callable, typename T , size_t... N_i>
void foo()
{
  using callable_out_type =  std::result_of_t< callable( /* T , ... , T <- sizeof...(N_i) many */ ) >;

  // ...
}

I want to get the result type of callable which takes sizeof...(N_i) many arguments of the type T as its input, e.g., callable(1,2,3) in case of T==int and sizeof...(N_i)==3. How can this be implemented?

Many thanks in advance.

like image 383
abraham_hilbert Avatar asked Oct 26 '16 17:10

abraham_hilbert


2 Answers

We can use a type alias to hook onto the expansion of N_i, but always give back T.

template <class T, std::size_t>
using eat_int = T;

template< typename callable, typename T , size_t... N_i>
void foo()
{
  using callable_out_type = std::result_of_t< callable(eat_int<T, N_i>...) >;

  // ...
}
like image 104
Quentin Avatar answered Nov 20 '22 06:11

Quentin


Why not simply use:

using callable_out_type = std::result_of_t< callable( decltype(N_i, std::declval<T>())...) >;

you could also use a trick borrowed from Columbo's answer:

using callable_out_type =  std::result_of_t< callable(std::tuple_element_t<(N_i, 0), std::tuple<T>>...) >;

or even:

using callable_out_type =  std::result_of_t< callable(std::enable_if_t<(N_i, true), T>...) >;
like image 6
W.F. Avatar answered Nov 20 '22 06:11

W.F.