Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3212416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:50:10+00:00 2026-05-17T14:50:10+00:00

I want to create a structure/class with a variable number of class members which

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:50:11+00:00Added an answer on May 17, 2026 at 2:50 pm

    With template metaprogramming and little preprocessing, it is possible to achieve a syntax close to desirable:

    //one has to "declare" once an attribute name to be able to use
    //it later in any number of class declarations
    DECLARE_ATTRIBUTE_NAME(foo);
    DECLARE_ATTRIBUTE_NAME(quux);
    DECLARE_ATTRIBUTE_NAME(bar);
    DECLARE_ATTRIBUTE_NAME(baz);
    
    //pass types and declared attribute names, separated by comma
    typedef TupleWithNamedMembers<int, foo,
                                  float, quux,
                                  double, bar,
                                  char, baz
                            > MyTuple;
    //extra call to macro "MAKE_TUPLE" can be avoided, see below
    class MyConstruct: public MAKE_TUPLE(MyTuple)
    { };
    
    //usage
    int main(int argc, char* argv[])
    {
        MyConstruct construct;
        construct.foo = 3;
        construct.bar = 5.6;
        construct.quux = 8.9;
        construct.baz = 'h';
        return 0;
    }
    

    The implementation:

    #ifndef TupleWithNamedMembersH
    #define TupleWithNamedMembersH
    //---------------------------------------------------------------------------
    
    #include <Loki/typelist.h>
    #include <Loki/HierarchyGenerators.h>
    
    template<class T, int a>
    struct attribute
    {
    };
    
    //the generated id is not really unique in all cases
    //one should provide better implementation
    #define GENERATE_UNIQ_ID(name) ((sizeof(#name)<<16)|__LINE__)
    
    //specializations of the struct "attribute" act like compile-time map between
    //a name ID and an attribute name 
    #define DECLARE_ATTRIBUTE_NAME_IMPL(name, id) \
        enum { id = GENERATE_UNIQ_ID(name) }; \
        template<class T> \
        struct attribute<T,id> \
        {\
            T name;\
        };
    #define DECLARE_ATTRIBUTE_NAME(name)\
        DECLARE_ATTRIBUTE_NAME_IMPL(name, name)
    
    //helps to pass pair of type and name ID as a single type
    template<class T, int i>
    struct pair
    {
        static const int val = i;
        typedef T type;
    };
    
    //unpacks compile-time data from PairT and inherits attribute
    //with name selected by ID
    template<class PairT>
    class holder: public attribute<typename PairT::type,PairT::val>
    {    };
    
    //turns template arguments into Loki::TypeList
    template
    <
        typename T1  = Loki::NullType, int i1 = 0, typename T2  = Loki::NullType, int i2 = 0,
        typename T3  = Loki::NullType, int i3 = 0, typename T4  = Loki::NullType, int i4 = 0,
        typename T5  = Loki::NullType, int i5 = 0, typename T6  = Loki::NullType, int i6 = 0,
        typename T7  = Loki::NullType, int i7 = 0, typename T8  = Loki::NullType, int i8 = 0,
        typename T9  = Loki::NullType, int i9 = 0, typename T10 = Loki::NullType, int i10 = 0
    >
    struct TupleWithNamedMembers
    {
    public:
        typedef Loki::TL::MakeTypelist<pair<T1,i1>, pair<T2,i2>,
                                       pair<T3,i3>, pair<T4,i4>,
                                       pair<T5,i5>, pair<T6,i6>,
                                       pair<T7,i7>, pair<T8,i8>,
                                       pair<T9,i9>, pair<T10,i10>
                             >::Result Result;
    };
    
    //this macro is required because of internal compiler error that I encounter
    //Loki::GenScatterHierarchy makes a class inherit every attribute from the type list
    #define MAKE_TUPLE(types) Loki::GenScatterHierarchy<types::Result, holder>
    
    #endif //end of "TupleWithNamedMembers.h"
    

    Notes:
    The MAKE_TUPLE macro should be a metafunction, if your compiler is OK with the code below:

    template
    <
        typename T1  = Loki::NullType, int i1 = 0, typename T2  = Loki::NullType, int i2 = 0,
        typename T3  = Loki::NullType, int i3 = 0, typename T4  = Loki::NullType, int i4 = 0,
        typename T5  = Loki::NullType, int i5 = 0, typename T6  = Loki::NullType, int i6 = 0,
        typename T7  = Loki::NullType, int i7 = 0, typename T8  = Loki::NullType, int i8 = 0,
        typename T9  = Loki::NullType, int i9 = 0, typename T10 = Loki::NullType, int i10 = 0
    >
    struct MakeTupleWithNamedMembers
    {
    private:
        typedef Loki::TL::MakeTypelist<pair<T1,i1>, pair<T2,i2>,
                                       pair<T3,i3>, pair<T4,i4>,
                                       pair<T5,i5>, pair<T6,i6>,
                                       pair<T7,i7>, pair<T8,i8>,
                                       pair<T9,i9>, pair<T10,i10>
                             >::Result type_list;
    public:
        typedef Loki::GenScatterHierarchy<type_list, holder> Result;
    };
    
    //usage
    class MyConstruct: public MakeTupleWithNamedMembers<int, foo, float, quux>::Result
    { };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having some trouble understanding variable scope within a class structure. I want to create
I want to create a structure which contains a list of same structure like
I want to create a class structure that can represent the following heirarchichal structure
I want to create a completely generic treeview like structure. some thing like this:
Example class User has_many :tickets end I want to create association which contains logic
I want to create a structure Degrees for a GPX library. In the XSD
I want to create tree structure using web service. I have used bottom up
I want to create a table structure with checkbox for each row and each
I am new to svg and dont know its structure.I want to create 10
Data table structure is: id1,id2,id3,id4,... (some other fields). I want to create summary query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.