Using Visual Studio 2010, I have:
using namespace std;
struct C
{
unique_ptr<F1, default_delete<F1>> Field1;
unique_ptr<F2, default_delete<F1>> Field2;
unique_ptr<FN, default_delete<F1>> FieldN;
}
It is going to be used in two contexts, CPU and GPU, in CPU context where the struct and the fields are going to have the default_delete and in the GPU context, with CUDA, where they are going to have a custom deleter which uses the function cudaFree to delete.
The custom deleter that might be used looks something like this
struct DevDeleter
{
void operator()(void* d_ptr)
{
cudaError_t error = cudaFree(d_ptr);
if (error != cudaSuccess)
{
throw;
}
}
}
So, my first hunch was to look at templating and my struct became:
template<typename Deleter>
struct C
{
unique_ptr<F1, Deleter> Field1;
unique_ptr<F2, Deleter> Field2;
unique_ptr<FN, Deleter> FieldN;
}
I have a framework of structs (more than 30) that need to work in 2 delete contexts. If I want to declare struct C in some function, this will have a recursive declaration, which can't be written:
unique_ptr<C<default_delete<C<default_delete<C<(recursive)>>>>, default_delete<C(recursive)>> c(new C<...>());
Do you have an improvement or a clean solution to allow a struct to have custom unique_ptr deleter for its members?
N.B. I am aware that I can do template specialization, but that is effectively duplicating the struct.