Use std::variant instead of std::any and std::array instead of std::unordered_map #26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This branch introduces a new container for parsed values and a more efficient retrieval method, replacing the current implementation.
In the existing code, parsed values are stored in a
std::unordered_map
and type-erased usingstd::any
. This approach has several drawbacks:std::any
for type erasure is inherently unsafe.std::unordered_map
indexed bystd::type_index
can involve costly lookups and may require resolving hash conflicts.This branch improves upon this by storing each parsed value in a
std::optional
with the actualValueType
specified in the option type. The values are stored in a contiguousstd::array
whose size matches the number of flags and options passed to the parser. To support multiple value types, the array’s underlying type is astd::variant
capable of holding any permutation ofstd::optional<ValueType>
.Consider the following example:
In this example, the value types are
int
,float
, andint
. Duplicate types are filtered out, so the container type becomes:Values are stored in the array in the order they were passed to the parser, ensuring type safety and eliminating hash lookups. Accessing values is as simple as indexing directly into the array, with the index determined at compile-time.
All logic related to this new container is encapsulated in a
ValueContainer
class, which is used by theParser
.Related Issues
Closes #3.