Currently, I manually create a lambda function for the new []
, as follows:
[](){ return new double[3]; }
This lambda is later used as a generator function in the call to std::generate
:
std::generate(vec.begin(), vec.end(), [](){ return new double[3]; });
Can I avoid the creation of this lambda and use some more standard functionality/syntax sugar/alternatives? C++17 is preferred; however, if C++20 (or some proposal) offers something better, I am very interested as well.
The long version provides some background, and an existing solution for a similar situation, when for_each
is used to deallocate the memory.
std::vector
that stores double*
pointers, which point to 1-D arrays of fixed size.std::vector<double*>
no matter how other containers might be better suited.for
-loops and decided to go for STL <algorithm>
.#include <vector>
int main()
{
// (1)
size_t numElems = 10;
std::vector<double*> vec(numElems);
for (size_t i=0; i<numElems; i++)
{
vec[i] = new double[3];
}
// (2)-(3) SOME ACTIVITY with vec
// (4)
for (size_t i=0; i<numElems; i++)
{
delete [] vec[i];
}
}
where, for simplicity, I hardcoded the numElems
in the vector vec
, and used hardcoded size of the double*
array to be 3
. The passing to external function and activity in (2)–(3) are skipped. This code works.
I tried the following:
#include <vector>
#include <algorithm>
#include <memory>
int main()
{
// (1)
size_t numElems = 10;
std::vector<double*> vec(numElems);
std::generate(vec.begin(), vec.end(), [](){ return new double[3]; });
// (2)-(3) SOME ACTIVITY with vec
// (4)
std::for_each(vec.begin(), vec.end(), std::default_delete<double[]>());
}
which also works fine.
In the revised version, the deletion is performed via std::default_delete<>
applied via std::for_each
that conveniently provides "a function representation" of the language keyword delete []
(a bit sloppy wording).
Is there a similar way "to represent" new []
? Or am I stuck creating the lambda manually to be used as a generator function argument for std::generate
?
The following:
#include <type_traits>
#include <vector>
#include <algorithm>
#include <memory>
template<typename T>
struct default_new {
typename std::decay<T>::type operator() (){ return new T; }
};
int main() {
size_t numElems = 10;
std::vector<double*> vec(numElems);
std::generate(vec.begin(), vec.end(), default_new<double[3]>());
// (2)-(3) SOME ACTIVITY with vec
// (4)
std::for_each(vec.begin(), vec.end(), std::default_delete<double[]>());
}
works. I do not know if such is provided from standard library, I believe not.