Classes on stack

Ali Çehreli acehreli at yahoo.com
Thu Sep 1 23:02:39 UTC 2022


On 9/1/22 12:45, IGotD- wrote:

 > an example that allocated the space in the class for
 > the member class

I am experimenting with a similar idea where the user can ask a type to 
have a buffer as a member:

struct S(T, size_t memberCapacity = size_t.max) {
   static if (memberCapacity != size_t.max) {
     // The user wants a member array
     T[memberCapacity] arr;

     void initMemberArray() {
       // Real work with 'arr'...
     }

   } else {
     void initMemberArray() {
       // No work needed in this case
     }
   }

   // ...
}

void main() {
   auto a = S!int();
   auto b = S!(int, 42)();

   pragma(msg, a.sizeof);  // 1
   pragma(msg, b.sizeof);  // 168
}

Ali



More information about the Digitalmars-d mailing list