What is the syntax for an array type?

Is this the type []? For example, can I

T<int[]>; 

for some template T.

+4
source share
5 answers

There are two syntaxes for designating array types. The first is the type-id syntax and is used wherever the language expects a compile-time type that looks like this:

 T[constant-expression] T[] 

Indicates the type of array, which in the first form has several elements specified by an integer constant expression (means that it must be known at compile time). In the second form, it sets the type of the array with an unknown number of elements. Like the types of classes that you declare without a body, this type of array is considered incomplete, and you cannot create arrays of this type

 // not valid: what size would it have? int a[]; 

However, you can specify this type. For example, you can type it

 typedef int unknown_int_array[]; 

In the same way, you can specify it as an argument of the template type, so the answer to your question is yes, you can pass this type to the specifier in the template. Note that I'm talking about qualifiers here because the form you use here is not the type itself.

The second way is to use the new-type-id syntax, which allows you to designate runtime types using variable boundaries

 T[expression] 

This allows you to transfer variables as the number of elements, and also allows you to transfer zero . In this case, an array of null elements is created. This syntax can only be used with the new operator to support dynamic arrays.

+3
source

The type "array of type T " is T [dimension] , which you can pass as template parameters. For instance:.

 someTemplate<int [10]> t; // array type as template parameter int a[5]; // array of 5 ints named 'a' 

Arrays must have a size that must be greater than 0. This means that, for example, U u[]; is illegal.

There are cases that may seem like exceptions, with the first parameters being:

 void f(T[]); 

This is a special rule for parameters, and f() actually equivalent to the following:

 void f(T*); 

Then there is a direct initialization of arrays:

 int a[] = { 1, 2, 3, 4 }; 

Here, the size of the array is implicitly specified through the number of elements in the initializer, so the type of a is int[4] .

There are also incomplete types of arrays without specific boundaries, but you cannot directly instantiate these files (see Johannes answer for more):

 template<class T> struct X { typedef T type; }; X<int[]>::type a = { 1, 2, 3 }; 

If you are looking for dynamic arrays, instead prefer standard containers like std::vector<T> .

+5
source

If possible, you can use dynamic arrays instead and pass the pointer as a template type. Such as the...

 T<int*> myVar; 
+2
source

It started as a comment on George, but he continued a little ...

It seems that you may not notice some key abstraction in your mental array model (at least in C style). Local arrays are allocated on the stack with a hard-coded size. If you have an array inside a class or structure, the space for the array is part of the object itself (whether on the stack or heap). Global arrays can even be displayed directly in the size of the executable file.

This means that anytime you want to use an array, you must specify its size for the compiler. The only reason you can leave the brackets empty in the parameter list is because functions treat array parameters as pointers. A function is unlikely to be useful if it can work with only one array size.

Templates are no exception. If you want the template size to be resized, you can add an additional template parameter. However, you should still indicate the size at compile time for any given instance.

+1
source

The syntax for declaring arrays is

 <type> <variable>[<size>]; 

When using the ad template, in the example

 template <class T> T var[4]; 
0
source

Source: https://habr.com/ru/post/1315743/


All Articles