2

I have a template to create instance of various objects. The template has a static array of class declaration which is supposed to create an array of class type being passed during its creation.

In below example myclass is the static array of class object with size 200 - which can be bigger also. Note that the template can be instantiated with different objects - so the type of array will also be changed accordingly.

How can initialize the static array during declaration itself - I understand that we need to initialize static array when defined itself, what if the size if more bigger -

template <class object>
A<object> myclass[200] = { .... new object 200 times...};

or I need it to do it new / delete overloaded operator as defined in template? In such a case how will the array of objects construction & destruction will occur? What if some object references the array as they are static before template instantiation?

Programmer
  • 8,303
  • 23
  • 78
  • 162
  • Your last paragraph is not quite clear to me. – R Sahu Aug 20 '17 at 04:05
  • Why does your class need a static array? I would probably refactor the class to accept a reference to a global singleton object of some kind. – Robinson Aug 20 '17 at 04:24
  • I have changed the question as requested - a static array is required cause of pre-determined data processing – Programmer Aug 20 '17 at 04:28
  • 1
    Ok, I suppose what you want is a Template Static Variable: https://stackoverflow.com/questions/1553854/template-static-variable. – Robinson Aug 20 '17 at 04:40
  • Thanks - there is an answer in link that we can have the static member data in base class - but how can we initialise a big static array of objects – Programmer Aug 20 '17 at 05:48

1 Answers1

0

How can initialise the static array during declaration itself [?]

If you want initialize the objects with the default (no parameter) contructor, it's easy; something like

template <class object>
object A<object>::myclass[200] { };

The following is a full (simplified) example

#include <iostream>

template <typename T, std::size_t Dim>
struct foo : public T 
 { static T const myArray[Dim]; };

template <typename T, std::size_t Dim>
T const foo<T, Dim>::myArray[Dim] { };

struct bar
 { bar () { std::cout << "bar! " << std::endl; } };

int main ()
 { (void)foo<bar, 10>::myArray; } // print 10 times "bar!"

If you want initialize it with a different constructor... it's a little more complicated.

The following is a C++14 example (use std::index_sequence and std::make_index_sequence; but isn't really difficult substitute they in C++11, if you need it) that use partial specialization, template default values, variadic parameters unpacking and the comma operator

#include <utility>
#include <iostream>

template <typename T, std::size_t Dim,
          typename U = decltype(std::make_index_sequence<Dim>{})>
struct foo;

template <typename T, std::size_t Dim, std::size_t ... Is>
struct foo<T, Dim, std::index_sequence<Is...>>
 { static T const myArray[Dim]; };

template <typename T, std::size_t Dim, std::size_t ... Is>
T const foo<T, Dim, std::index_sequence<Is...>>::myArray[Dim]
    { ((void)Is, T(1))... };

struct bar
 { bar (int) { std::cout << "bar! " << std::endl; } };

int main ()
 { (void)foo<bar, 10>::myArray; } // print 10 times "bar!"
max66
  • 65,235
  • 10
  • 71
  • 111