I want to create a structure/class with a variable number of class members which could be decided at compilation stage (like done in template metaprogramming)
Example : Its hypothetical in which both type and variable names are to be specified like Type T1 variable name should be varName1 and so on …..
template <class T1 (varName1) >
MyClass
{
T1 varName1;
}
template <class T1 (varName1), class T2 (varName2) >
MyClass
{
T1 varName1;
T1 varName2;
}
and in main code which can be declared like following or some other way in which type and name can be specified
MyClass Obj;
and MyClass::somefunc() can access variable names
as follows
MyClass::somefunc()
{
std::cout <<" abc value : " << abc << std::endl;
std::cout <<" xyz value : " << xyz << std::endl;
}
Is this possible via template metaprogramming in C++ to have both type and variable name specification ?
With template metaprogramming and little preprocessing, it is possible to achieve a syntax close to desirable:
The implementation:
Notes:
The MAKE_TUPLE macro should be a metafunction, if your compiler is OK with the code below: