Home > Net >  parameterized C nested struct array initialization
parameterized C nested struct array initialization

Time:05-17

I've checked posts here that I can use template for nested struct. But when I'm trying to initialize an array inside a nested struct, there seems problem during initialization. In the following example, the array size is one of the parameters of the nested struct so Visual Studio complained that the array size is illegal. Here is the definition:

//template<typename U, typename T>
template<typename U, size_t T>   // this line also not work
struct A {
    struct B {
        struct C {
            vector<U> count[T];    // count needs to have variable size 
            C() {        
                count = new U[T];  // allocate size. Not work
            }
        C c;
    };
    B b;
};

Did I do anything wrong when using the template and initialize the array?

Thanks

CodePudding user response:

There are 3 problems with your current code.

Problem 1

You're missing the closing braces and semicolon }; corresponding to the struct A. To solve this just add }; corresponding to struct A as shown below.

Problem 2

T is a template type parameter and hence cannot be used to specify the size of an array. The simplest way of solving this is make T as template nontype parameter as shown below.

Problem 3

count is an array of vectors in your example so count = new U[T]; doesn't make sense. Looking at your comment, you were trying to create count as a vector with elements of type U and size T which can be done as shown below.

Additionally, note that we can't have C c; inside class type C and similarly we can't have B b; inside B. This is because we cannot have a non-static data member of incomplete type inside a class.


Solution

//-------------------vvvvvvvvvvv---->nontype parameter
template<typename U, std::size_t T>
struct A {
    struct B {
        struct C {
            std::vector<U> count; //count is a std::vector    
            C(): count(T) {        
                  std::cout<<"size of count vector is: "<<count.size()<<std::endl;
            }
        //C c;//this won't work here as C is INCOMPLETE at this point
        };//--------->C is complete after this point 
        
        C c; //THIS WORKS HERE because C is COMPLETE at this point
       // B b;//this wont work here as B is INCOMPLETE at this point
    };//------------->B is complete after this point

    B b; //THIS WORKS HERE because B is INCOMPLETE at this point 
};//added this  

Working demo

CodePudding user response:

Did I do anything wrong when using the template and initialize the array?

Yes, you do count = new U[T];, but count is not a pointer.

If you want the vector to be initialized to have the size T, provide T to the vector's constructor in the member initializer list:

template<typename U, size_t T>
struct A {
    struct B {
        struct C {
            std::vector<U> count;
            C() : count(T) {}  // now `count´ is constructed with `T` elements
        };                     // <- you are also missing this line
        C c;
    };
    B b;
};

If you really want a fixed size array of T vector<U>s:

#include <array>
template<typename U, size_t T>
struct A {
    struct B {
        struct C {
            std::array<std::vector<U>, T> count;
        };
        C c;
    };
    B b;
};
  •  Tags:  
  • c
  • Related