@@ -11,32 +11,24 @@ void getSizeType(const %1%<Types...> &container, size_t& returnArg);
11
11
"""
12
12
13
13
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
-
34
14
template<class... Types>
35
15
void getSizeType(const %1%<Types...> &container, size_t& returnArg)
36
16
{
37
17
SAVE_SIZE(sizeof(%1%<Types...>));
38
-
39
18
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);
41
33
}
42
34
"""
0 commit comments