Skip to content

Commit 2e74fa3

Browse files
committed
std::variant: change to use std::visit
1 parent 512163f commit 2e74fa3

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

types/std_variant.toml

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,24 @@ void getSizeType(const %1%<Types...> &container, size_t& returnArg);
1111
"""
1212

1313
func = """
14-
template <size_t I = 0, class... Types>
15-
void getSizeVariantContents(const %1%<Types...> &container, size_t& returnArg)
16-
{
17-
if constexpr (I < sizeof...(Types))
18-
{
19-
if (I == container.index())
20-
{
21-
// Contents are stored inline - don't double count
22-
SAVE_SIZE(-sizeof(std::get<I>(container)));
23-
24-
getSizeType(std::get<I>(container), returnArg);
25-
}
26-
else
27-
{
28-
getSizeVariantContents<I+1>(container, returnArg);
29-
}
30-
}
31-
// else variant is valueless - save no data
32-
}
33-
3414
template<class... Types>
3515
void getSizeType(const %1%<Types...> &container, size_t& returnArg)
3616
{
3717
SAVE_SIZE(sizeof(%1%<Types...>));
38-
3918
SAVE_DATA(container.index());
40-
getSizeVariantContents(container, returnArg);
19+
20+
// This check should be `container.valueless_by_exception()` but it doesn't
21+
// work with the variable sized integers used in `std::variant`. For fewer
22+
// than 256 options it uses a `uint8_t` index but checks against -1 of
23+
// `uintptr_t`. Manually check for any out of bounds indices as a workaround.
24+
if (container.index() >= sizeof...(Types)) {
25+
return;
26+
}
27+
28+
std::visit([&returnArg](auto &&arg) {
29+
// Account for inline contents
30+
SAVE_SIZE(-sizeof(arg));
31+
getSizeType(arg, returnArg);
32+
}, container);
4133
}
4234
"""

0 commit comments

Comments
 (0)