c++ - 使用自定义删除器为 unique_ptr 返回 nullptr 失败

标签 c++ templates compiler-errors c++14

当我尝试编译这段代码时,它失败了: “错误:静态断言失败:使用空函数指针删除器构造”。它提示的那一行是“return nullptr”。返回 nullptr 与我编写的其他返回 unique_ptr 的函数一起使用。为什么它如此不同甚至无法编译?

#include <iostream>
#include <memory>

template<typename ArrayType>
void deleter(ArrayType* array) noexcept
{
    if (array)
    {
        delete[] array;
        array = nullptr;
        std::cout << "Freed array." << std::endl;
    }
}
template<typename ArrayType>
std::unique_ptr<ArrayType[], decltype(&deleter<ArrayType>)> makeArray(const std::size_t size)
{
    return std::unique_ptr<ArrayType[], decltype(&deleter<ArrayType>)>{
        new ArrayType[size],
        deleter<ArrayType>
    };
}

std::unique_ptr<int[], decltype(&deleter<int>)> createInt(int s)
{
    if (s == 3)
        return makeArray<int>(3);
    else
        return nullptr;
}

void testArr(int arr[])
{
    if (arr != nullptr)
    {
        arr[0] = 1;
        arr[1] = 2;
        arr[2] = 3;
        std::cout << "Value 2 is " << arr[1] << std::endl;
    }
    else
        std::cout << "Array is empty." << std::endl;
}

int main() {
    auto arr0{createInt(4)}, arr1{createInt(3)};
    std::cout << "\tTesting arr0:\n";
    testArr(arr0.get());
    std::cout << "\tTesting arr1:\n";
    testArr(arr1.get());
    std::cout << "\tFinished testing\n";
    return 0;
}

最佳答案

看来您需要使用函数对象,例如:

template<typename ArrayType>
struct deleter {
    void operator()(ArrayType* array) {
    if (array)
    {
        delete[] array;
        array = nullptr;
        std::cout << "Freed array." << std::endl;
    }
    }
};

看这里的例子:

http://coliru.stacked-crooked.com/a/172693cdc5704531

我认为标准的相关部分在 20.8.1.2.1 [unique.ptr.single.ctor]

返回你调用的 nullptr :unique_ptr& operator=(nullptr_t) noexcept; 调用 constexpr unique_ptr() noexcept;,它又在备注中有:

Remarks: If this constructor is instantiated with a pointer type or reference type for the template argument D, the program is ill-formed.

关于c++ - 使用自定义删除器为 unique_ptr 返回 nullptr 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679008/

相关文章:

c++ - 在编译时映射两种类型

c++ - 完美转运不取货方式

C++如何识别无符号类型?

android - 在android开发项目之间共享源文件

c++ - GetClickablePoint 不适用于 UI 自动化

c++ - ISO C++ 禁止声明没有类型的 'getRawAccValues' [-fpermissive]

c++ - 阵列平衡点

c++ - 从 QVariantList 获取字符串

python - 如何为 MacOS for python 3.9 解决 "Preparing wheel metadata ... error"这个错误

visual-c++ - 32 个不应该发生的错误