0

I would like to create class members through a variadic template so that a template defined like this:

template<typename firstType, firstName, Rest... rest>
class MyClass{
    FirstType firstName;
    //do something recursvy: https://stackoverflow.com/questions/7230621/how-can-i-iterate-over-a-packed-variadic-template-argument-list
};

with the following instance:

class MyClass<int A, float B, char c>

would generate a class like this:

class MyClass{
    int A;
    float B;
    char c;
}

How do I do this? or something equivalent like a tuple.

I can only picture doing this with nasty C macros.

Jordan McBain
  • 275
  • 2
  • 11
  • 3
    If you want the nested type names to match the template parameter names, it can't be done with standard C++ afaik. What's your use case? What's wrong with `std::tuple`? – joergbrech Mar 13 '23 at 17:00
  • I don't think there is anything wrong with tuple? – Jordan McBain Mar 13 '23 at 17:36
  • Judging from your question alone, it seems like you want `MyClass` to be a compound of several types passed as template arguments. `std::tuple` is just that. So is there anything you want to achieve, that you can't with `std::tuple`? – joergbrech Mar 13 '23 at 21:18
  • String based name access without string comparison. I guess I might be able to build an enum with the template parameters – Jordan McBain Mar 15 '23 at 00:31

0 Answers0