vendredi 27 mars 2015

How do I reserve space on the stack for a non-default constructible?


I would basically write the following piece of code. I understand why it can't compile.



A instance; // A is a non-default-constructable type and therefore can't be allocated like this

if (something)
{
instance = A("foo"); // use a constructor X
}
else
{
instance = A(42); // use *another* constructor Y
}

instance.do_something();


Is there a way to achieve this behaviour without involving heap-allocation?




Aucun commentaire:

Enregistrer un commentaire