Consider this example on Compiler explorer.
Basically, we have this code snippet:
#include <cstdint>
#include <variant>
enum class Enum1 : std::uint8_t { A, B };
enum class Enum2 : std::uint8_t { C, D };
using Var = std::variant< Enum1, Enum2 >;
using Var2 = std::variant< char >;
template< std::size_t s >
struct print_size;
void func() {
print_size< sizeof( Var ) >{};
print_size< sizeof( Var2 ) >{};
}
If we compile this with GCC's libstdc++ (using either clang or GCC), we get the expected compile error:
error: implicit instantiation of undefined template 'print_size<2>'
Also, similar with MSVC (as expected):
error C2027: use of undefined type 'print_size<2>'
However, when using clang with libc++, I get this error:
error: implicit instantiation of undefined template 'print_size<8>'
This indicates that sizeof( std::variant< char > ) == 8
when using libc++. I've confirmed that on Linux (see compiler explorer link above), but also with Android's NDK r18 and Xcode 10 (both iOS and MacOS).
Is there a reason for libc++'s implementation of std::variant
to use so much memory or is this simply a bug in libc++ and should be reported to libc++ developers?
__index
and__data
and I don't remember why but__index
is usually unsigned int` which explains the size see godbolt – Rancorous