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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:04:13+00:00 2026-05-20T18:04:13+00:00

In standard c++ we can write : int myArray[5] = {12, 54, 95, 1,

  • 0

In standard c++ we can write :

int myArray[5] = {12, 54, 95, 1, 56};

I would like to write the same thing with a template :

Array<int, 5> myArray = {12, 54, 95, 1, 56};

assuming that

template <class Type, unsigned long N>
class Array
{
public:

    //! Default constructor
    Array();

    //! Destructor
    virtual ~Array();

    //! Used to get the item count
    //! @return the item count
    unsigned long getCount() const;

    //! Used to access to a reference on a specified item
    //! @param the item of the item to access
    //! @return a reference on a specified item
    Type & operator[](const unsigned long p_knIndex);

    //! Used to access to a const reference on a specified item
    //! @param the item of the item to access
    //! @return a const reference on a specified item
    const Type & operator[](const unsigned long p_knIndex) const;

private:

    //! The array collection
    Type m_Array[N];
};

I thinks it is not possible but may be there’s a tricky way to do it !

  • 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-20T18:04:14+00:00Added an answer on May 20, 2026 at 6:04 pm

    My solution is to write a class template that accumulates all the values which get passed to the constructor. Here is how you can initizalize your Array now:

    Array<int, 10> array = (adder<int>(1),2,3,4,5,6,7,8,9,10);
    

    The implementation of adder is shown below with complete demonstration:

    template<typename T>
    struct adder
    {
       std::vector<T> items;
       adder(const T &item) { items.push_back(item); }
       adder& operator,(const T & item) { items.push_back(item); return *this; }
    };
    
    template <class Type, size_t N>
    class Array
    {
    public:
    
        Array(const adder<Type> & init) 
        {
             for ( size_t i = 0 ; i < N ; i++ )
             {
                   if ( i < init.items.size() )
                       m_Array[i] = init.items[i];
             }
        }
        size_t Size() const { return N; }
        Type & operator[](size_t i) { return m_Array[i]; }
        const Type & operator[](size_t i) const { return m_Array[i]; }
    
    private:
    
        Type m_Array[N];
    };
    
    int main() {
    
            Array<int, 10> array = (adder<int>(1),2,3,4,5,6,7,8,9,10);
            for (size_t i = 0 ; i < array.Size() ; i++ )
               std::cout << array[i] << std::endl;
            return 0;
    }
    

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

    See the online demo at ideone yourself : http://www.ideone.com/KEbTR

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

Sidebar

Related Questions

Are there WinAPI functions that do the same what can do standard Windows utility
We can initialize a container deque by using standard input like this: deque<int> c((istream_iterator<int>(cin)),(istream_iterator<int>()));
Can you write something like this in C with multiple assignment operations? int a
How can I write a Standard C( C89 or C99 compliant) macro that is
I know that in standard SQL you can do this: update top (100) table1
Does the system come with a standard control that can be used for drawing
How can i open a standard print dialog (like in Word/Wordpad) to print an
Learning C++ and see the class laid out like this: class CRectangle { int
I am trying to write server that will communicate with any standard client that
hello everyone I'm trying to write function which can unfold int in the list

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.