On Fri, Feb 24, 2023 at 09:50:33AM +0000, Jonathan Wakely wrote: > On Fri, 24 Feb 2023 at 09:49, Jakub Jelinek wrote: > > > > Assuming a compiler handles the T m_vecdata[1]; as flexible array member > > like (which we need because standard C++ doesn't have flexible array members > > nor [0] arrays), I wonder if we instead of the m_auto followed by m_data > > trick couldn't make auto_vec have > > alignas(vec<vl_embed>) unsigned char buf m_data[sizeof (vec<vl_embed>) + (N > > - 1) * sizeof (T)]; > > and do a placement new of vec<vl_embed> into that m_data during auto_vec > > construction. Isn't it then similar to how are flexible array members > > normally used in C, where one uses malloc or alloca to allocate storage > > for them and the storage can be larger than the structure itself and > > flexible array member then can use storage after it? > > You would still be accessing past the end of the > vec<vl_embed>::m_vecdata array which is UB.
Pedantically sure, but because C++ doesn't have flexible array members, people in the wild use the flexible array member like arrays for that purpose. If there was T m_vecdata[];, would it still be UB (with the flexible array member extensions)? We could use T m_vecdata[]; if the host compiler supports them and T m_vecdata[1]; otherwise in the hope that the compiler handles it similarly. After all, I think lots of other real-world programs do the same. Jakub