vendredi 26 décembre 2014

Can't invoke new operator for list initialization with a std::initializer_list?

So, with C++11, we got a new form of operator new:



auto dynamicArray = new int[5]{1, 2, 3, 4, 5};


The so called "list-initialized" format.


My question is whether or not it's possible to call this form of the new operator using a std::initializer_list, as in:



std::initializer_list<int> initializer = {1, 2, 3, 4, 5};
auto dynamicArray = new int[5] initializer; //Doesn't work


Wrapping initializer in braces also doesn't work (conversion from std::initializer_list<int> to int). Adding parenthesis also does not seem to help.


I am aware that it is possible to use variadic templates to create a similar effect (and avoid using std::initializer_list altogether), but would prefer to avoid this solution as the forwarding references create some fun when it comes to overload resolution on the function I'm using this in.


I am also aware that I can use std::malloc and std::free to get uninitialized memory and do the filling myself with std::uninitialized_copy.


Aucun commentaire:

Enregistrer un commentaire