template<class T, auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
class IndexedArray< T, FirstIndex, LastIndex, Size >
An std::array
-like class that supports some additional features:
- Can be indexed with an enum (type is inferred from the
FirstIndex
parameter).
- Supports an
std::map
-like initialization, so that the user doesn't have to manually double check that the order of the values in the initializer matches the order of the values of the enum that's used for indexing.
- Supports non-zero-based indexing (e.g. can be used to construct a Pascal-like array).
The template itself can be used in several different ways:
IndexedArray<int, 1, 10>
creates a Pascal-like array with a first index of 1, and last index of 10 (so the size is still 10 as in the previous example).
IndexedArray<int, FirstWeaponItem, LastWeaponItem>
creates a non-zero-based enum-indexed array.
Some code examples:
enum class TriBool {
True,
False,
DontKnow
};
using enum TriBool;
{True, "true"},
{False, "false"},
{DontKnow, "unknown"}
};
extern TriBool f(int);
std::cout << "f(10)=" << defaultMessageMap[f(10)] << std::endl;
for (auto &value : userMessageMap)
value.clear();
for (TriBool
i : defaultMessageMap.
indices())
userMessageMap[
i] = defaultMessageMap[
i];
i
Definition: Json_ut.cpp:82
Definition: IndexedArray.h:66
constexpr Segment< key_type > indices() const
Definition: IndexedArray.h:143
- Template Parameters
-
T | Array element type. |
FirstIndex | Index of the first element. Value must be of enum or integral type. |
LastIndex | Index of the last element. The size of the indexed array is LastIndex - FirstIndex + 1 . |
template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
Creates an uninitialized indexed array.
This is the constructor that gets called when you use aggregate initialization, so the behavior is the same as with std::array
:
If you want to default-initialize array elements, see the other constructor.