使用Cang的pahole
工具,这就是结果
class optional<int> : _Optional_base<int, true, true> {
public:
};
struct _Optional_base<int, true, true> : _Optional_base_impl<int, std::_Optional_base<int, true, true> > {
struct _Optional_payload<int, true, true, true> _M_payload; /* 0 8 */
};
class _Optional_base_impl<int, std::_Optional_base<int, true, true> > {
protected:
};
struct _Optional_payload<int, true, true, true> : _Optional_payload_base<int> {
};
struct _Optional_payload_base<int> {
union _Storage<int, true> _M_payload; /* 0 4 */
bool _M_engaged; /* 4 1 */
};
union _Storage<int, true> {
struct _Empty_byte _M_empty; /* 0 0 */
int _M_value; /* 0 4 */
};
struct _Empty_byte {
};
《龙珠》:https://godbolt.org/z/r4KTfYKoW
归根结底这相当于
class optional<int> {
int value;
bool used;
};