Is it possible to convert a C++ struct's fields to a pack parameter?

149 Views Asked by At

I have a struct like below:

struct A1
{
   int a;
   int b;

   char c[10];
};

or

struct A2
{
   double t;    
   char* p;
};

And now I want to access all filed, My leader suggest me to use pack parameter to handle this simply, just like this:

template<typename... T>
void handler(T...args)
{
   // handle all struct fileds, such a, b, c here
}

// how to call it?

A1 a1;    
handle(a1);

A2 a2;    
handle(a2);

Sadly, I have no idea about that how to access all fields one by one?

Please note that the template function should be general for we don't know how the struct is defined?

Is it possible? if yes, Could you please show me you idea? Thanks a lot.

1

There are 1 best solutions below

0
On BEST ANSWER

I believe the answer is no.

A paper proposing a way to do this (via structured bindings) has been written, but it won't be considered until at least November.