sincerely Singaporean

If you have not done so, read this full tutorial on how to use SGEXTN to build an application.

SGLArray

see header file

(no source file, everything inside header)

template ‹typename T› class SGLArray;

part of SGEXTN module SG_Containers

variable length array for any type of data

detailed description

list of all including inherited members

implementation details

preprocessor file inclusion directive: #include ‹SGLArray.h›

CMake target for BuildLah: SGEXTN::SG_Containers

see this link for more information about BuildLah

parent class: (none)

children classes: (none)

instance member functions

SGLArray(int count, const T& defaultValue);

SGLArray(Ts... data);

SGLArray(int count);

void assign(int count, const T& defaultValue);

[[nodiscard]] const T& at(int i) const;

[[nodiscard]] T& at(int i);

void fill(const T& defaultValue);

[[nodiscard]] int length() const;

[[nodiscard]] const T* pointerToData(int n) const;

[[nodiscard]] T* pointerToData(int n);

Detailed Description

SGLArray provides a template based array data structure to store any type of data. This is similar to C arrays and different from C++'s std::array as the size does not need to be a compile time constant. This is a template based class with no separate source file. This class is a SGEXTN container. Copy constructor, copy assignment, move constructor, move assignment, and destructor work as expected. A deep copy is performed whenever this class is copied, and the new instance will not be linked to the old instance in any way. It is assumed that the contents placed into this SGEXTN container can be copied and moved. This means that their copy constructor copy assignment, move constructor, move assignment, and destructor work as expected. If this is not the case or if you want the container to store references or constant references, store pointers instead.

Implementation Details

SGLArray internally maintains a C array stored on the heap.

SGLArray(int count, const T& defaultValue);

Creates a SGLArray of size count, with every element initialised to defaultValue.

You must ensure that count is nonnegative. Negative count will crash.

SGLArray(Ts... data);

SGLArray does not support initialisation using initialiser lists. This is the closest that you can get to using a initialiser list.

This constructor of SGLArray takes a variable number of arguments corresponding to what the data inside should be set to. The length of the SGLArray is determined automatically from the number of arguments passed.

The produced SGLArray will contain all the arguments passed in order to the variadic template data, for example SGLArray‹int›(1, 2, 3, 4, 5) will create a SGLArray‹int› with contents [1, 2, 3, 4, 5]

The arguments to this constructor must be within a pair of parentheses, not braces. This is NOT a initialiser list.

This constructor has lower function overload resolution priority than any other constructor. When using this to initialise a SGLArray containing 1 or 2 numerical data types, ensure that this does not resolve to another constructor unintentionally.

SGLArray(int count);

Creates a SGLArray of size count, with every element default initialised.

The type stored in the SGLArray must have a default constructor (the one taking no arguments) for this to compile.

You must ensure that count is nonnegative. Negative count will crash.

void assign(int count, const T& defaultValue);

Assigns a SGLArray(count, defaultValue) to this SGLArray.

arr.assign(count, defaultValue); is identical in functionality to arr = SGLArray‹T›(count, defaultValue); Choosing one over the other is purely coding style.

You must ensure that count is nonnegative. Negative count will crash.

[[nodiscard]] const T& at(int i) const;

Returns a constant reference to element i of the SGLArray.

This will crash for out of bounds i.

[[nodiscard]] T& at(int i);

Returns a reference to element i of the SGLArray.

Since this returns a reference and not a copy, assigning to it directly (using it as a lvalue) would modify the SGLArray.

This will crash for out of bounds i.

void fill(const T& defaultValue);

Sets every element in the existing SGLArray to defaultValue without resizing the SGLArray.

If the SGLArray may need to be resized, use SGLArray::assign instead.

[[nodiscard]] int length() const;

Returns the length of the SGLArray, which is the number of elements stored inside it.

Unless assigned to or if SGLArray::assign is used, this always stays the same after the SGLArray is created.

[[nodiscard]] const T* pointerToData(int n) const;

Returns a read only pointer to element n of the SGLArray. This does not dereference the returned pointer.

Using an out of bounds value for n is ok for this function as long as the returned pointer is not dereferenced. Out of bounds values for n may be used intentionally to specify parts of the SGLArray to run SGLSort on.

The returned value may function as an iterator.

Dereferencing the returned pointer when n is out of bounds results in undefined behaviour.

[[nodiscard]] T* pointerToData(int n);

Returns a pointer to element n of the SGLArray. This does not dereference the returned pointer.

Using an out of bounds value for n is ok for this function as long as the returned pointer is not dereferenced. Out of bounds values for n may be used intentionally to specify parts of the SGLArray to run SGLSort on.

The returned value may function as an iterator.

Assigning to the dereferenced value from the returned pointer would modify the SGLArray

Dereferencing the returned pointer when n is out of bounds results in undefined behaviour.

©2025 05524F.sg (Singapore)

contact 05524F / report a bug / make a suggestion

about 05524F SINGAPORE values

list of 05524F projects