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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:29:14+00:00 2026-06-15T16:29:14+00:00

Sounds like an easy one but I can’t figure a suitable solution to this:

  • 0

Sounds like an easy one but I can’t figure a suitable solution to this: For register allocator I need a counter that starts counting from 0 and increments on each allocation step.

Ok, lets make this a general problem (not specific to register allocation): I need a class that can have multiple instances (that’s important!) and that has a templated member function that returns an integer which’s value is counting up on each call. The interface should look like:

class Counter
{
public:
  template<class T>
  int plus1() {
    // ?
  }
private:
  // what member ?
};

When one uses the counter it should function like this:

int main() {
  Counter a,b;

  assert( a.plus1<int>() == 0);    
  assert( a.plus1<int>() == 1);    

  assert( b.plus1<float>() == 0);  
  assert( b.plus1<float>() == 1);  

  assert( a.plus1<float>() == 0);  
}

Obviously when relaxing the “multiple instances” requirement this can be implemented with a static int local variable . However I need multiple instances, and I think this makes the whole thing tricky.

* SOLUTION/EDIT *

I think @log0 gave the right solution. Here for completeness the full working C++11 code (at least it seems to work):

class Counter
{
public:
  template<class T>
  int plus1() {
    return counters[ std::type_index( typeid(T) ) ]++;
  }
private:
  std::map<std::type_index, int> counters;
};
  • 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-06-15T16:29:15+00:00Added an answer on June 15, 2026 at 4:29 pm

    You can use type_index (c++11)

    class Counter
    {
    public:
      template<class T>
      int plus1() {
        return map_[std::type_index(typeid(T))]++;
      }
    private:
      std::map<std::type_index, int> map_;
    };
    

    typeid is deduced at compile time if not called on reference to polymorphic object.

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

Sidebar

Related Questions

I know this sounds like a dumb question but I need to ask this.
This may sounds like a stupid question but can't find anything on google, probably
This sounds like a simple enough question, but can't find the answer for the
I know this sounds like an easy question but for some reason I just
This sounds like a really simple question, but I am new to PHP. If
This sounds like a problem many others have posted about, but there's a nuance
I know this sounds like a homework assignment, but it isn't. Lately I've been
This probably sounds like a terrible idea at first glance, but here is my
This sounds daft and easy but I'm not 100% sure here. I have this
Sounds like a stupid question, doesn't it? But the page on the Jettison project

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.