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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:10:45+00:00 2026-06-18T07:10:45+00:00

I have a template class that accepts from 1 to 8 integer arguments. The

  • 0

I have a template class that accepts from 1 to 8 integer arguments. The permitted range for each argument is 0..15. A default value of 16 for each argument allows me to detect unused arguments.

I would like to have the number of user-supplied arguments available as a compile-time constant. I can do this using a template helper class and a lot of partial specialisation.

My question is, can I clean this up using a bit of recursive metaprogramming. What I have works but it feels like it could be improved syntactically.

Variadic templates and anything else c++0x are not available to me, sadly.

#include <stdint.h>
#include <iostream>

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4,uint8_t p5,uint8_t p6,uint8_t p7>
struct Counter { enum { COUNT=8 }; };

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4,uint8_t p5,uint8_t p6>
struct Counter<p0,p1,p2,p3,p4,p5,p6,16> { enum { COUNT=7 }; };

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4,uint8_t p5>
struct Counter<p0,p1,p2,p3,p4,p5,16,16> { enum { COUNT=6 }; };

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4>
struct Counter<p0,p1,p2,p3,p4,16,16,16> { enum { COUNT=5 }; };

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3>
struct Counter<p0,p1,p2,p3,16,16,16,16> { enum { COUNT=4 }; };

template<uint8_t p0,uint8_t p1,uint8_t p2>
struct Counter<p0,p1,p2,16,16,16,16,16> { enum { COUNT=3 }; };

template<uint8_t p0,uint8_t p1>
struct Counter<p0,p1,16,16,16,16,16,16> { enum { COUNT=2 }; };

template<uint8_t p0>
struct Counter<p0,16,16,16,16,16,16,16> { enum { COUNT=1 }; };


template<uint8_t p0,uint8_t p1=16,uint8_t p2=16,uint8_t p3=16,
         uint8_t p4=16,uint8_t p5=16,uint8_t p6=16,uint8_t p7=16>
struct MyClass {

  void printArgCount() {
    std::cout << Counter<p0,p1,p2,p3,p4,p5,p6,p7>::COUNT << std::endl;
  }
};


main() {
  MyClass<4,7,8,12,15,1> foo;

  foo.printArgCount();
}
  • 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-18T07:10:46+00:00Added an answer on June 18, 2026 at 7:10 am

    Why not just checking the first 16 that appears ?

    template <uint8_t p0, ...>
    struct Counter {
        enum { COUNT = (p1 == 16 ? 1 : 
                        p2 == 16 ? 2 :
                        p3 == 16 ? 3 :
                        p4 == 16 ? 4 :
                        p5 == 16 ? 5 :
                        p6 == 16 ? 6 :
                        p7 == 16 ? 7 :
                                   8
                       ) };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class
Say I have a template function that accepts variable arguments and calls other functions
I have a class that accepts a list of policy classes using boost::mpl. Each
I want to have a template class that looks something like what I have
I have a custom iterator template class that wraps up a std::list iterator. In
I have a template class with a variadic template member function that I am
I have a templated container class that looks something like this: template <class ItemType>
I have the following use case: There's a class called Template and with that
I have code that looks like this: template<class T> class list { public: class

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.