Skip to content

Instantly share code, notes, and snippets.

@jonwis
Created August 12, 2023 03:36
Show Gist options
  • Save jonwis/2fb5449307399476dabd65f4a95373cc to your computer and use it in GitHub Desktop.
Save jonwis/2fb5449307399476dabd65f4a95373cc to your computer and use it in GitHub Desktop.
Bad use of initializer-list
#include <span>
template<typename T> struct bad_span
{
bad_span() : m_data(nullptr), m_size(0) {}
bad_span(std::initializer_list<T> value) :
m_data(value.begin()),
m_size(static_cast<uint32_t>(value.size()))
{
}
auto& operator[](uint32_t i) const
{
return m_data[i];
}
T const* m_data;
uint32_t m_size;
};
struct type_oops {
type_oops(int);
~type_oops();
void foo() const;
};
void doom()
{
auto b = bad_span<type_oops>({1, 2, 3});
b[0].foo();
}
void doom(void) PROC ; doom
$LN17:
sub rsp, 40 ; 00000028H
mov edx, 1
lea rcx, QWORD PTR $T1[rsp]
call type_oops::type_oops(int) ; type_oops::type_oops
npad 1
mov edx, 2
lea rcx, QWORD PTR $T1[rsp+1]
call type_oops::type_oops(int) ; type_oops::type_oops
npad 1
mov edx, 3
lea rcx, QWORD PTR $T1[rsp+2]
call type_oops::type_oops(int) ; type_oops::type_oops
npad 1
lea r9, OFFSET FLAT:type_oops::~type_oops(void) ; type_oops::~type_oops
mov edx, 1
lea r8d, QWORD PTR [rdx+2]
lea rcx, QWORD PTR $T1[rsp]
call void `eh vector destructor iterator'(void *,unsigned __int64,unsigned __int64,void (__cdecl*)(void *))
lea rcx, QWORD PTR $T1[rsp]
call void type_oops::foo(void)const ; type_oops::foo
add rsp, 40 ; 00000028H
ret 0
void doom(void) ENDP ; doom
@jonwis
Copy link
Author

jonwis commented Aug 12, 2023

For folks playing along from home, here's the Godbolt link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment