unique_ptr <T> Specjalny separator lambda dla specjalizacji tablicy [duplikat]

To pytanie ma już odpowiedź tutaj:

Jak korzystać z niestandardowego narzędzia do usuwania z elementem std :: unique_ptr? 6 odpowiedzi

Niedawno zacząłem przenosić wiele mojego istniejącego kodu aplikacji C ++ na C ++ 11, a teraz, kiedy przechodzę na nowe inteligentne wskaźnikistd :: unique_ptr istd :: shared_ptr Mam konkretne pytanie dotyczące niestandardowych programów do usuwania. Chcę dodać rejestrator lambda, aby zobaczyć, gdzie wywoływane są moje usunięcia, ale nie mogę skompilować wersji specjalizacji tablic. Porady będą bardzo mile widziane.

Na próżno szukałem przykładu niestandardowego narzędzia do specjalizacji tablic unique_ptr dla VC ++ 10 lubGCC 4.5.2 +. Chciałbym wydrukować komunikat w dzienniku, gdy usuwacze są wywoływane w lambda - głównie po to, aby upewnić się, że robią to wszystkie wskaźniki, które moim zdaniem wykraczają poza zakres. Czy jest to możliwe w przypadku specjalizacji tablicowej? Mogę zmusić go do pracy z wersją inną niż tablica, a także mogę sprawić, by działał ze specjalizacją tablic, jeśli przekażę zewnętrzną strukturę „MyArrayDeleter” jako drugi argument. Co więcej, czy można byłoby usunąć brzydkistd :: function ponieważ myślałem, że mógłbym to rozpozna

struct MySimpleDeleter {
    void operator()(int* ptr) const {
        printf("Deleting int pointer!\n");
        delete ptr;
    }
};
struct MyArrayDeleter {
    void operator()(int* ptr) const {
        printf("Deleting Array[]!\n");
        delete [] ptr;
    }
};
{
    // example 1 - calls MySimpleDeleter where delete simple pointer is called
    std::unique_ptr<int, MySimpleDeleter> ptr1(new int(5));

    // example 2 - correctly calls MyArrayDeleter where delete[] is called
    std::unique_ptr<int[], MyArrayDeleter> ptr2(new int[5]);

    // example 3 - this works (but default_delete<int[]> would have been passed
    // even if I did not specialize it as it is the default second arg
    // I only show it here to highlight the problem I am trying to solve
    std::unique_ptr<int[], std::default_delete<int[]>> ptr2(new int[100]);

    // example 3 - this lambda is called correctly - I want to do this for arrays
    std::unique_ptr<int, std::function<void (int *)>> ptr3(
        new int(3), [&](int *ptr){ 
            delete ptr; std::cout << "delete int* called" << std::endl; 
        });

    // example 4 - I cannot get the following like to compile
    // PLEASE HELP HERE - I cannot get this to compile
    std::unique_ptr<int[], std::function<void (int *)>> ptr4(
        new int[4], [&](int *ptr){  
            delete []ptr; std::cout << "delete [] called" << std::endl; 
        });
}

The compiler error is as follows:

The error from the compiler (which complains about the new int[4] for ptr4 below is:
'std::unique_ptr<_Ty,_Dx>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty,_Dx>'
1>          with
1>          [
1>              _Ty=int [],
1>              _Dx=std::tr1::function<void (int *)>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2513) : see declaration of 'std::unique_ptr<_Ty,_Dx>::unique_ptr'
1>          with
1>          [
1>              _Ty=int [],
1>              _Dx=std::tr1::function<void (int *)>
1>          ]

questionAnswers(2)

yourAnswerToTheQuestion