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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:54:00+00:00 2026-06-12T12:54:00+00:00

I have a class that is templated with ints (i.e.: template </*…*/, int a>

  • 0

I have a class that is templated with ints (i.e.:

template </*...*/, int a> /*...*/

). In my class, I would like a constructor that takes exactly “a” arguments. I can of course make it variadic, but I’d like compile-time checks on length if possible. I also think macro hacks could work, but I’m starting by looking for built-in C++ functionality.

Is this possible in C++, and how can it done if so?

  • 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-12T12:54:01+00:00Added an answer on June 12, 2026 at 12:54 pm

    Dealing with a sequence of values of the same type is what arrays are for.

    You don’t even need to use raw arrays; with C++11 you can use std::array.

    E.g. like so:

    template< int a >
    class MyClass
    {
    public:
        MyClass( std::array< int, a > const& args )
        {}
    };
    

    If your compiler doesn’t offer std::array then you can very easily define a corresponding class, or you can just use a raw array:

    template< int a >
    class MyClass
    {
    public:
        MyClass( int const (&args)[a] )
        {}
    };
    

    Hm, I hope I got the placement of & correct there. For some reason that I can’t fathom, I always forget that syntax. No matter how many times I’ve used it.


    Given that the OP clarifies in a comment that (1) he doesn’t have C++11 and (2) he wants simple declaration syntax like

    MyClass<4> m(0,1,2,3);
    

    one possibility is to make MyClass an aggregate that can be initialized by C++03 curly braces initializer, i.e., no user defined constructor:

    #include <stddef.h>
    
    typedef ptrdiff_t Size;
    typedef Size Index;
    
    template< Size a >
    class MyClass
    {
    public:
        static Size const n = a;
        static Size size() { return n; }
    
        int elems_[n];
        int operator[]( Index const i ) const { return elems_[i]; }
        int& operator[]( Index const i ) { return elems_[i]; }
    };
    
    #include <iostream>
    using namespace std;
    int main()
    {
        MyClass< 3 > x = {100, 200, 300};
        for( int i = 0;  i < x.size(); ++i )
        {
            wcout << x[i] << endl;
        }
    }
    

    If this solution is acceptable then it’s essentially to reimplement std::array.

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

Sidebar

Related Questions

I have a templated container class that looks something like this: template <class ItemType>
I have a templated class, that can have a map type as template parameter.
I have my templated container class that looks like this: template< class KeyType, class
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I want to have a template class that looks something like what I have
I have code that looks like this: template<class T> class list { public: class
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class
I have a templated class that is dependent on two template parameters to calculate
I have been trying to create a templated class( Test2 ) that takes 2
I'm building a hashmap class that can have string keys and ints, bools, strings

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.