On Fri, Aug 07, 2020 at 09:34:38AM +0100, Jonathan Wakely via Gcc-patches wrote:
> > Now that you say it, vec has a T[1] member so depending on T
> > there might be a side-effect as invoking its CTOR?  Or it might
> > even not compile if there is no default CTOR available...  Ick :/
> 
> Right.
> 
> Does the GTY stuff add members to the struct, or otherwise alter its
> layout?

Or perhaps use offsetof on an alternate structure that should have the same
layout.
So instead of
  typedef vec<T, A, vl_embed> vec_embedded;
  return offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
use
  struct alignas (T) U { char data[sizeof (T)]; };
  typedef vec<U, A, vl_embed> vec_embedded;
  return offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
where vec_embedded should have the same offset of m_vecdata as vec.

Proof of concept:
template <typename T>
struct vec
{
  int a;
  T m_vecdata[1];
};

template <typename T>
int
foo (int alloc)
{
//  typedef vec<T> vec_embedded;
  struct alignas (T) U { char data[sizeof (T)]; };
  typedef vec<U> vec_embedded;
  return __builtin_offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
}

struct V { };
struct S : virtual V { long long l; S (); ~S (); };

int a = foo <S> (0);

        Jakub

Reply via email to