Variable length struct in C++
since it’s a POD, everything works just like it did in C. - SO
- Multiple return values (structured bindings) with unmovable types and guaranteed RVO in C++17
- Array-to-pointer decay - The only “connection” between T[n] and T[m] is that both types can implicitly be converted to T*, and the result of this conversion is a pointer to the first element of the array.
struct var
{
int a;
int b[1];
static std::shared_ptr<var> make_var(int num_b) {
const extra_bytes = (num_b ? num_b-1 : 0)*sizeof(int);
return std::shared_ptr<var>(
new char[sizeof(var)+extra_bytes ],
[](var* p){delete[]((char*)(p));});
}
Written on June 3, 2021, Last update on June 13, 2021
c++
array