3

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 !

Péter Török
  • 114,404
  • 31
  • 268
  • 329
chris
  • 91
  • 1
  • 1
  • 6

5 Answers5

5

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

Nawaz
  • 353,942
  • 115
  • 666
  • 851
2

This becomes possible in C++0x using initializer lists. Currently, there is no way to do this.

The closest you can get without this is to use Boost.Assign.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
1

It's actually very trivial; just remove the constructors and make the data members public. The template issue is a red hering; the same rules apply as for any class: if it is an aggregate, you can use aggregate initialization; if it's not, you can't.

-- James Kanze

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • If I set the data members as public this will break the access security implemented by the template class. – chris Mar 21 '11 at 09:30
  • @chris Yes. If hiding the elements is critical, then you can't use this format. In a lot of cases, the array is part of the abstraction, so making it public is no issue. In others, the array is totally hidden, so even wanting to use aggregate initialization is a violation of the abstraction. In the cases between those two extremes, you're a bit stuck. – James Kanze Mar 21 '11 at 09:58
1

Yet another solution which doesn't need adder class template. Now you can do this:

int main() {

        Array<int, 10> array;
        array = 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

Here is the complete solution: http://www.ideone.com/I0L1C

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    @nawaz ideone link is broken. could you post the code? thanks – Koushik Shetty Jan 08 '14 at 16:43
  • @Koushik: I've posted two solutions. See the other one which has the implementation of the `Array`. You can use that and experiment yourself on ideone. :-) – Nawaz Jan 08 '14 at 17:40
  • @Nawaz Actually i think i know how you have implemented but just wanted to be sure so.:-) very nice and this is the first time i saw anyone overloading ,.+1. – Koushik Shetty Jan 08 '14 at 17:48
0

You're right. This is not possible with current standard C++. However, with the next standard (c++0x) initializer-lists will do just that!

ltjax
  • 15,837
  • 3
  • 39
  • 62