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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:19:05+00:00 2026-05-25T20:19:05+00:00

I have the following code : template<size_t sz,typename T=float> class Vec{ T v[sz]; Vec(const

  • 0

I have the following code :

template<size_t sz,typename T=float> class Vec{
    T v[sz];    
    Vec(const T& val,const T&... nv){
        //how do i assign `sz` number of first arguments into `this->v` array
    }
}

I want to create constructor, that receive generic number of constructor argument, and assign the first sz number of arguments into member variable of v

what I want to do, is to be able doing like this: Vec<3> var(1.0,2.0,3.0);

  • 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-25T20:19:05+00:00Added an answer on May 25, 2026 at 8:19 pm

    This is possible, but complicated. Here is some code that does it. It may be possible to eliminate the holder type, but I leave that as an exercise for the reader. This has been tested with g++ 4.6.

    #include <iostream>
    #include <typeinfo>
    
    template<size_t ... Indices> struct indices_holder
    {};
    
    template<size_t index_to_add,typename Indices=indices_holder<> >
    struct make_indices_impl;
    
    template<size_t index_to_add,size_t...existing_indices>
    struct make_indices_impl<index_to_add,indices_holder<existing_indices...> >
    {
        typedef typename make_indices_impl<
            index_to_add-1,
            indices_holder<index_to_add-1,existing_indices...> >::type type;
    };
    
    template<size_t... existing_indices>
    struct make_indices_impl<0,indices_holder<existing_indices...> >
    {
        typedef indices_holder<existing_indices...>  type;
    };
    
    template<size_t max_index>
    typename make_indices_impl<max_index>::type make_indices()
    {
        return typename make_indices_impl<max_index>::type();
    }
    
    template<unsigned index,typename ... U>
    struct select_nth_type;
    
    template<unsigned index,typename T,typename ... U>
    struct select_nth_type<index,T,U...>
    {
        typedef typename select_nth_type<index-1,U...>::type type;
    
        static type&& forward(T&&,U&&... u)
        {
            return select_nth_type<index-1,U...>::forward(static_cast<U&&>(u)...);
        }
    };
    
    template<typename T,typename ... U>
    struct select_nth_type<0,T,U...>
    {
        typedef T type;
    
        static type&& forward(T&&t,U&&...)
        {
            return static_cast<T&&>(t);
        }
    };
    
    template<unsigned index,typename ... U>
    typename select_nth_type<index,U...>::type&& forward_nth(U&&... u)
    {
        return static_cast<typename select_nth_type<index,U...>::type&&>(
            select_nth_type<index,U...>::forward(
                static_cast<U&&>(u)...));
    }
    
    template<size_t sz,typename T=float> struct Vec{
        struct holder
        {
            T data[sz];
        };
    
        holder v;
    
        template<typename ... U>
        struct assign_helper
        {
            template<size_t... Indices>
            static holder create_array(indices_holder<Indices...>,Vec* self,U&&... u)
            {
                holder res={{static_cast<T>(forward_nth<Indices>(u...))...}};
                return res;
            }
        };
    
        template<typename ... U>
        Vec(U&&... u):
            v(assign_helper<U...>::create_array(make_indices<sz>(),this,static_cast<U&&>(u)...))
        {}
    };
    
    int main()
    {
        Vec<3> v(1.2,2.3,3.4,4.5,5.6,7.8);
    
        std::cout<<"v[0]="<<v.v.data[0]<<std::endl;
        std::cout<<"v[1]="<<v.v.data[1]<<std::endl;
        std::cout<<"v[2]="<<v.v.data[2]<<std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: template <class T> static std::string ToString(const T& t) {
I have the following code: template <typename T> LuaCall& operator>>(T) { BOOST_STATIC_ASSERT(sizeof(T) == 0);
I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I have the following code: class TimeOutException {}; template <typename T> class MultiThreadedBuffer {
I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray
I have something like the following code: template<typename T1, typename T2, typename T3, typename
Inside my template function I have the following code: TypeName myFunction() { TypeName result;
I have the following code: template <int size> inline uint hashfn( const char* pStr
Consider the following simplified version of my code. I have a template class A

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.