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 683863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:45:16+00:00 2026-05-14T01:45:16+00:00

I am attempting to create a template AutoClass that create an arbitrary class with

  • 0

I am attempting to create a template “AutoClass” that create an arbitrary class with an arbitrary set of members, such as:

AutoClass<int,int,double,double> a;
a.set(1,1);
a.set(0,2);
a.set(3,99.7);
std::cout << "Hello world! " << a.get(0) << " " << a.get(1) << " " << a.get(3) << std::endl;

By now I have an AutoClass with a working “set” member:

class nothing {};

template <  typename T1 = nothing, typename T2 = nothing, typename T3 = nothing,
            typename T4 = nothing, typename T5 = nothing, typename T6 = nothing>
class AutoClass;

template <>
class AutoClass<nothing, nothing, nothing,
                nothing, nothing, nothing>
{
    public:
    template <typename U> void set(int n,U v){}
};

template <  typename T1, typename T2, typename T3,
            typename T4, typename T5, typename T6>
class AutoClass: AutoClass<T2,T3,T4,T5,T6>
{
    public:
    T1 V;
    template <typename U> void set(int n,U v)
    {
        if (n <= 0)
            V = v;
        else
            AutoClass<T2,T3,T4,T5,T6>::set(n-1,v);
    }
};

and I started to have problems implementing the corresponding “get”. This approach doesn’t compile:

template <  typename T1, typename T2, typename T3,
            typename T4, typename T5, typename T6>
class AutoClass: AutoClass<T2,T3,T4,T5,T6>
{
    public:
    T1 V;
    template <typename U> void set(int n,U v)
    {
        if (n <= 0)
            V = v;
        else
            AutoClass<T2,T3,T4,T5,T6>::set(n-1,v);
    }
    template <typename W> W get(int n)
    {
        if (n <= 0)
            return V;
        else
            return AutoClass<T2,T3,T4,T5,T6>::get(n-1);
    }
    template <> T1 get(int n)
    {
        if (n <= 0)
            return V;
        else
            return AutoClass<T2,T3,T4,T5,T6>::get(n-1);
    }
};

Besides, it seems I need to implement get for the <nothing, nothing, nothing, nothing, nothing, nothing> specialization. Any Idea on how to solve this?

  • 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-14T01:45:16+00:00Added an answer on May 14, 2026 at 1:45 am

    First of all, I prefer Boost.Fusion to Boost.Tuple as it supports a better mixin of template metaprogramming and runtime algorithms I think.

    For example, I’d like to present you a little marvel:

    struct Name {}; extern const Name name;
    struct GivenName {}; extern const GivenName givenName;
    struct Age {}; extern const Age age;
    
    class Person
    {
    public:
      template <class T>
      struct value
      {
        typedef typename boost::fusion::result_of::at_key<data_type const,T>::type type;
      };
    
      template <class T>
      struct has
      {
        typedef typename boost::fusion::result_of::has_key<data_type,T>::type type;
      };
    
      template <class T>
      typename value<T>::type
      get(T) { return boost::fusion::at_key<T>(mData); }
    
      template <class T>
      Person& set(T, typename value<T>::type v)
      {
        boost::fusion::at_key<T>(mData) = v; return *this;
      };
    
    private:
      typedef boost::fusion::map <
        std::pair<Name, std::string>,
        std::pair<GivenName, std::string>,
        std::pair<Age, unsigned short>
      > data_type;
      data_type mData;
    };
    

    It’s really fun to use:

    Person p;
    p.set(name, "Rabbit").set(givenName, "Roger").set(age, 22);
    

    Well, I myself prefer indexing by classes than by indices, because I can convey meaning as well as adding type checking 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create a php template that includes object oriented code. Here's a
I'm attempting to create a Parcelable class in Android so that I can pass
I am attempting to create an overloaded operator for a matrix class that I
I am attempting to create my 1st Simple DLL. I have a class(that's a
I am attempting to create a template that will utilize data from an Access
I am attempting to create a vector iterator within a template class I am
I am attempting to create a simple base class and another class that inherits
I am attempting to create a report, utilizing a matrix, that only displays columns
I am attempting to create a Tab Control Style that basically looks like buttons
I'm attempting create a spinning reload button, such as the one found in the

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.