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

  • Home
  • SEARCH
  • 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 1113105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:48:20+00:00 2026-05-17T02:48:20+00:00

I have the following problem: Suppose I have some basic counter class Counter .

  • 0

I have the following problem:

Suppose I have some basic counter class Counter. And suppose we also have some sets of classes, that can be counted. Let’s name some of them class CountedA and class CountedB.

Now, every class, which can be counted (such as CountedA and CountedB) has the following statically declared parts: one enum and one int part, that acts like a part of counted data.

For example, it’s declaration could look the following way:

enum CountedType { A, B };

template <CountedType Type, int N>
class Counted { };

// Now we can declare 'CountedA' and 'CountedB'
typedef Counted<A, 25> CountedA;
typedef Counted<B, 7> CountedB;

Now, the declaration of the counter:

// C++0x variadic or simply bunch of 'typename XX' definitions for C++03
template <typename T0, typename T1, typename ...>
class Counter
{
   // I don't know how to implement this
   // for now!
   int GetTotalN() { ... }

   // Retrieve the corresponding type
   // so that GetTypeAt<0> returns
   // enum from 'T0'
   template <int Pos>
   CountedType GetTypeAt() { ... }
};

I want to be able to write something like:

class RealCounter : public Counter<CountedA, CountedB> { };

And use it the following way:

RealCounter counter;
int n = counter.GetTotalN();
CountedType type = counter.GetTypeAt<0>();

Now, I’m pretty sure that this can be done. But what’s the best way of implementing it? (don’t ask me why would I need such crazy kind of things 🙂

Does boost::mpl offer something for this case?

Thank you.


Small update:

In this particular example, GetTotalN() should return 25 + 7.

If we add, for example, typedef Counted<C, 2> CountedC, then the result for

RealCounter : public Counter<CountedA, CountedB, CountedC>

should become 25 + 7 + 2.

  • 1 1 Answer
  • 3 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-17T02:48:21+00:00Added an answer on May 17, 2026 at 2:48 am

    Here’s C++03 code which works (for up to 10 template arguments). The main trick is giving class Counter a multiple inheritance, and passing objects of type Counter to function templates which must select a base class. The actual summation is done recursively.

    Counter.hpp

    enum CountedType { A, B };
    
    template <CountedType Type, int N>
    struct Counted {};
    
    struct DummyCounted {};
    
    template <int Pos, typename T>
    struct IndexedType {};
    
    template <unsigned int Terms>
    struct PartialSum
    {
      template <typename CounterT>
      static int getSum(const CounterT& ctr)
      { return PartialSum<Terms-1>::getSum(ctr) + ctr.template GetNAt<Terms>(); }
    };
    
    template <> struct PartialSum<0U>
    {
      template <typename CounterT>
      static int getSum(const CounterT& ctr)
      { return ctr.template GetNAt<0>(); }
    };
    
    template <typename T0, typename T1=DummyCounted,
      typename T2=DummyCounted, typename T3=DummyCounted,
      typename T4=DummyCounted, typename T5=DummyCounted,
      typename T6=DummyCounted, typename T7=DummyCounted,
      typename T8=DummyCounted, typename T9=DummyCounted>
    class Counter :
      public IndexedType<0, T0>, public IndexedType<1, T1>,
      public IndexedType<2, T2>, public IndexedType<3, T3>,
      public IndexedType<4, T4>, public IndexedType<5, T5>,
      public IndexedType<6, T6>, public IndexedType<7, T7>,
      public IndexedType<8, T8>, public IndexedType<9, T9>
    {
    public:
      static int GetTotalN() {
        return PartialSum<9>().getSum( Counter() );
      }
    
      template <int Pos>
      static CountedType GetTypeAt() { return _getTypeAt<Pos>( Counter() ); }
    
      template <int Pos>
      static int GetNAt() { return _getNAt<Pos>( Counter() ); }
    
    private:
      template <int Pos, CountedType Type, int N>
      static CountedType _getTypeAt(const IndexedType<Pos, Counted<Type,N> >&)
      { return Type; }
    
      template <int Pos, CountedType Type, int N>
      static int _getNAt(const IndexedType<Pos, Counted<Type,N> >&)
      { return N; }
    
      template <int Pos>
      static int _getNAt(const IndexedType<Pos, DummyCounted>&)
      { return 0; }
    
    };
    

    Counter.cpp

    #include "Counter.hpp"
    #include <iostream>
    
    typedef Counted<A, 25> CountedA;
    typedef Counted<B, 7> CountedB;
    
    class RealCounter : public Counter<CountedA, CountedB> {};
    
    int main()
    {
      RealCounter counter;
      int n = counter.GetTotalN();
      CountedType type = counter.GetTypeAt<0>();
    
      std::cout << "n is " << n
                << "\ntype check is " << (type == A) << std::endl;
      return 0;
    }
    

    Output:

    n is 32
    type check is 1
    

    That C++0x variadic template stuff looks interesting, but I haven’t taken a good look at it yet. But I do think in C++0x, all this example’s functions (except main of course) could be constexpr.

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

Sidebar

Related Questions

Propose to consider the following problem. Suppose we have some composite object. Are there
Hi i have following Problem. I write a Mediawiki Extension where i need some
I'm having some problems with deserializing an object. I have following classes: Metadatastore: [DataContract]
Suppose I have a following constructor in C++ class: MyClass::MyClass() { char* buffer =
I have a problem with a javascript global variable, let's suppose I have the
I have the following problem. I have a data set that has the beginning
Suppose I have the following code: namespace x{ class X{ virtual void x(){} }
Suppose I have some string, and run the following tests on it: response.indexOf(</p:panelGrid>); response.matches(.*</p:panelGrid>.*);
He're an interesting problem that looks for the most Pythonic solution. Suppose I have
I have following problem: I have to make a ASP.NET Webapplication with a two-row

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.