Xavier
Xavier

Reputation: 9049

How to initialize an array when using a template

I created an array template for my personal use.

template <typename T, int size>
struct Vector {
    T data[size];
};

I tried to intialize the data like so:

Vector<unsigned char, 10> test;
test.data[] = {0,1,2,3,4,5,6,7,8,9};

My compiler ended up complaining something about "expected expression." Does anyone know what I'm doing? I want to be able to use this style of initialization where you give it the entire array definition at once instead of using a for loop to init the elements individually.

Upvotes: 0

Views: 1143

Answers (4)

Filip Ros&#233;en
Filip Ros&#233;en

Reputation: 63797

In the new standard, C++11, you can use std::initalizer_list to get the desired result, see the below example.

#include <iostream>
#include <algorithm>

template <typename T, int size>
struct Vector {
  T data[size];

  Vector<T, size> (std::initializer_list<T> _data) {
    std::copy (_data.begin (), _data.end (), data);
  }
  // ...

  Vector<T, size>& operator= (std::initializer_list<T> const& _data) {
    std::copy (_data.begin (), _data.end (), data);
    return *this;
  } 
};

int
main (int argc, char *argv[])
{
  Vector<int, 10> v ({1,2,3,4,5,6}); // std::initializer_list

  v = {9,8,7,6,5,4,3,2,1,0}; // operator=
}

If you are working with a standard prior to C++11 it's a bit more of a hassle really, and your best bet is to implement functions similar to those available when using std::vector.

#include <iostream>
#include <algorithm>

template <typename T, int size>
struct Vector {
  T _data[size];

  Vector (T* begin, T* end) {
    std::copy (begin, end, _data);
  }   

  // ...

  void assign (T* begin, T* end) {
    std::copy (begin, end, _data);
  }   
};  

int 
main (int argc, char *argv[])
{   
  int A1[4] = {1,2,3,4};
  int A2[5] = {99,88,77,66,55};

  Vector<int, 10> v1 (A1, A1+4);

  // ...

  v1.assign (A2, A2+5);
} 

Upvotes: 2

DevSolar
DevSolar

Reputation: 70213

You can only initialize an array at the point you are defining it:

Vector<unsigned char, 10> test;

There's your array, you are done defining it, your chance to initialize it has passed.

Edit: Seeing Mat's answer, memo to me: I have to read up on C++11, and soon... :-/

Edit 2: I just gave the information on what was wrong. Kerrek SB has the information on how to do it right. ;-)

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 476930

Since your class is an aggregate, you can initialize it with the usual brace syntax:

Vector<int, 3> x = { { 1, 2, 3 } };

The exact same thing applies to std::array<int, 3>.

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409136

You have to supply the type and size of the array when defining the variable:

Vector<int, 10> test;

You can not however assign to the member array like a normal array. You have to assign each element separately:

for (int i = 0; i < 10; i++)
    test.data[i] = i;  // If instantiated with type "int"

Upvotes: 0

Related Questions