Cannot move std::unique_ptr with NULL deleter to std::shared_ptr?

前端 未结 2 937
刺人心
刺人心 2021-01-22 12:36

I want to move a NULL std::unique_ptr to a std::shared_ptr, like so:

std::unique_ptr test = nullptr;
std::shared_ptr          


        
2条回答
  •  春和景丽
    2021-01-22 13:41

    I'll echo Brian's answer and add that in situations like this where the function shouldn't be null, you can use a function reference, which are nonnullable like all C++ references, instead of function pointers.

    void delete_float(float *f) {delete f;}
    
    using Deleter = void(float*);
    
    // Function pointers
    constexpr void(*fptr)(float*) = delete_float;
    constexpr Deleter *fptr_typedef = delete_float;
    constexpr auto fptr_auto = delete_float;
    constexpr auto *fptr_auto2 = delete_float;
    
    // Function references
    constexpr void(&fref)(float*) = delete_float;
    constexpr Deleter &fref_typedef = delete_float;
    constexpr auto &fref_auto = delete_float;
    

    The one gotcha you need to keep in mind with function references is that lambdas implicitly convert to function pointers, but not function references, so you need to use the dereference operator * to convert a lambda to a function reference.

    const Deleter *fptr_lambda = [](float *f) {delete f;};
    // const Deleter &fref_lambda = [](float *f) {delete f;}; // error
    const Deleter &fref_lambda_fixed = *[](float *f) {delete f;};
    

提交回复
热议问题