SecreC 2 language  2.8.0 (2023.09)
Language and standard library reference
Data structures

Data structures in SecreC

Data structures

SecreC supports basic data structures currently intended to be used for returning multiple values from functions and for simple aggregation of data. The types of structure fields is not restricted in any way: it's possible to store other structures, arrays and private data within a structure. Structures themselves, however, are limited in multiple ways. Namely, they are restricted to be public data and may not not be stored as elements of arrays.

For example, to two-dimensional integer elements can be implemented as a structure with two integer fields x and y.

Listing 1: Example of a data structure definition

struct elem2d {
int x;
int y;
}
public elem2d v;
v.x = 1;
v.y = 2;

The language also supports polymorphic structures. A structure may have various type-level parameters that all need to be specified whenever a structure with that name is used. The previous structure can be declared type-polymorphically in which case, when defining data of that type, the type parameter has to be specified as well.

Listing 2: Example of a type-polymorhic data structure

template <type T>
struct elem2d {
T x;
T y;
}
public elem2d<int> v;
v.x = 1;
v.y = 2;

Structures may also be polymorphic over protection domains.

Listing 3: Example of a domain-polymorphic data structure

template <domain D, type T>
struct elem2d {
D T x;
D T y;
}
public elem2d<pd_shared3p, int> v;
v.x = 1;
v.y = 2;