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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:20:15+00:00 2026-05-26T06:20:15+00:00

I am training my template skills in C++ and want to implement a vector

  • 0

I am training my template skills in C++ and want to implement a vector class.
The class is defined by the vector dimension N and the type T.
Now I would like to have a constructor that takes exactly N variables of type T.
However I can’t get my head around how to tell the variadic template to only accept N parameters. Maybe this is possible with template specialization?
Or am I thinking in the wrong direction?
Any thoughts/ideas on this would be greatly appreciated.

More thoughts

All examples on variadic templates I already saw used recursion to “iterate” through the parameter list. However I have in mind that constructors can not be called from constructors (read the comments in the answer). So maybe it is not even possible to use variadic templates in constructors? Anyway that would only defer me to the usage of a factory function with the same basic problem.

  • 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-26T06:20:15+00:00Added an answer on May 26, 2026 at 6:20 am

    A variadic constructor seems appropriate:

    template<typename T, int Size>
    struct vector {
        template<typename... U>
        explicit
        vector(U&&... u)
            : data {{ std::forward<U>(u)... }}
        {
            static_assert( sizeof...(U) == Size, "Wrong number of arguments provided" );
        }
    
        T data[Size];
    };
    

    This example uses perfect forwarding and and static_assert to generate a hard-error if not exactly Size arguments are passed to the constructor. This can be tweaked:

    • you can turn the hard-error into a soft-error by using std::enable_if (triggering SFINAE); I wouldn’t recommend it
    • you can change the condition to be sizeof...(U) <= Size, letting the remaining elements to be value initialized
    • you can require that the types passed to the constructor are convertible to T, or exactly match e.g. T const&; either turning a violation into a hard-error (using static_assert again) or a soft-error (SFINAE again)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For training, I'm trying to write a class like std::string. I have read that
I'm training code problems like UvA and I have this one in which I
Possible Duplicate: trailing return type using decltype with a variadic template function I want
I am writing a template Polynom<T> class where T is the numeric type of
I have this tiny Qt project with a project file like this: TEMPLATE =
I am in the training of web developement.I have seen in most of the
I have been asked to find training resources to bring engineers up to speed
I want to create a template for all my python scripts using this autocmd
I have an xml/tei like <p> In trattoria scoprii che c'era <del rend=tratto a
I have 100 training, lecture, guide video files which has size 30MB to 1GB

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.