Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

trouble with tuple

jimdempseyatthecove
Honored Contributor III
366 Views

I consider myself a noob with regards to variadic template programming.

In the std there is the template tuple. In which you can create the tuple with any number of and mixture of type. This is quite handy (except for the reversed order which is a minor annoyance).

The problem I am trying to work out is in accessing programmicly tuple entries. Pseudo code:

template<typename... T>
inline // avoid duplicate symbols
void qt_unwrap_fn_args(std::tuple<void(*) (T...), T...>* t)
{
  auto fn = std::get<0>(t);
  // now call the enclosed functor with a variable number of args
  // 4 args directly would be this way:
  fn(
    std::get<1>(t),
    std::get<2>(t),
    std::get<3>(t),
    std::get<4>(t)
  );
// but i'd prefer to do it with ... expansion
// due to the fact that the arguments vary in number
// I am having difficulty in figuring out how to have the template expansion generate the
// correct number of arguments together with literal get number
  fn(
    std::get<T sequence number + 1>(t)... // expands to std::get<1>(t),..., std::get<4>(t)
  );
}

As an unrelated issue the above sketch shows obtaining the functor via std::get<0)(t), on my system V16.0 update 2 this does not return the first arg (last in memory). I have to retype case t, and then extract the functor that way. I will leave this issue for another day.

The object of the code is to convert a packet (POD) into a function call together with arguments.

 Jim Dempsey

0 Kudos
8 Replies
Judith_W_Intel
Employee
366 Views

 

Would this work (needs to compile in -std=c++14 mode iwith a library version of GNU 4.9 and later):

#include <tuple>
#include <stdio.h>

void foo(int i, int j, int k, int l) {
   printf("i %d j %d\n",i,j);
}

template<typename Function, typename Tuple, size_t ... I>
void call(Function f, Tuple t, std::index_sequence<I ...>)
{
     f(std::get<I+1>(t)...);
}

template<typename... T>
inline
void qt_unwrap_fn_args(std::tuple<void(*) (T...), T...> t)
{
  auto fn = std::get<0>(t);
  // now call the enclosed functor with a variable number of args
  // 4 args directly would be this way:
  fn(
     std::get<1>(t),
     std::get<2>(t),
     std::get<3>(t),
     std::get<4>(t)
  );

  static constexpr auto size = sizeof...(T);
  call(fn,t, std::make_index_sequence<size>{});
}

int main() {
    qt_unwrap_fn_args(std::make_tuple(foo,5,6,7,8));
    return 0;
}

0 Kudos
jimdempseyatthecove
Honored Contributor III
366 Views

Thanks for looking at this.

Unfortunately I am using C++11 :(

Thanks anyway. I will keep this in mind when Parallel Studio offers C++14

Jim Dempsey

0 Kudos
KitturGanesh
Employee
366 Views

Hi Judy,
Is this an issue that needs to be fixed or will work in the next 16.0 update?
Thanks,
Kittur

0 Kudos
Judith_W_Intel
Employee
366 Views

 

Kittur -- so far I have not seen a bug report, just a usability question. There is some mention of an unrelated issue but I don't really understand what the problem is so Jim if you want to post that in a separate thread with a test case that shows the problem that would be great...

0 Kudos
Judith_W_Intel
Employee
366 Views

 

Jim

This might be helpful in getting it to work in C++11:

http://stackoverflow.com/questions/10766112/c11-i-can-go-from-multiple-args-to-tuple-but-can-i-go-from-tuple-to-multiple

I'm not sure what version of Parallel Studio you are using but 16.0 does not support c++14 (except for variable templates and relaxed constexpr neither of which are needed). But of course we don't supply our own library so you would have to be on an operating system that had std::index_sequence.

Good luck!

Judy

 

0 Kudos
KitturGanesh
Employee
366 Views

Thanks Judy, understood. 

Jim, BTW, 17.0 Beta release (https://software.intel.com/en-us/forums/intel-c-compiler/topic/623321) supports most of C++14, fyi.

Regards,
Kittur

0 Kudos
jimdempseyatthecove
Honored Contributor III
366 Views

I may give that a try. I have a new topic that I will post after this message. Please take a look

Jim Dempsey

0 Kudos
KitturGanesh
Employee
366 Views

Thanks, Jim. Let me know what you find with 17.0 when you try out.

Kittur

0 Kudos
Reply