diff --git a/types/std_variant.toml b/types/std_variant.toml index c9c72ef..df157cd 100644 --- a/types/std_variant.toml +++ b/types/std_variant.toml @@ -11,32 +11,24 @@ void getSizeType(const %1% &container, size_t& returnArg); """ func = """ -template -void getSizeVariantContents(const %1% &container, size_t& returnArg) -{ - if constexpr (I < sizeof...(Types)) - { - if (I == container.index()) - { - // Contents are stored inline - don't double count - SAVE_SIZE(-sizeof(std::get(container))); - - getSizeType(std::get(container), returnArg); - } - else - { - getSizeVariantContents(container, returnArg); - } - } - // else variant is valueless - save no data -} - template void getSizeType(const %1% &container, size_t& returnArg) { SAVE_SIZE(sizeof(%1%)); - SAVE_DATA(container.index()); - getSizeVariantContents(container, returnArg); + + // This check should be `container.valueless_by_exception()` but it doesn't + // work with the variable sized integers used in `std::variant`. For fewer + // than 256 options it uses a `uint8_t` index but checks against -1 of + // `uintptr_t`. Manually check for any out of bounds indices as a workaround. + if (container.index() >= sizeof...(Types)) { + return; + } + + std::visit([&returnArg](auto &&arg) { + // Account for inline contents + SAVE_SIZE(-sizeof(arg)); + getSizeType(arg, returnArg); + }, container); } """