当我尝试编译这段代码时,它失败了: “错误:静态断言失败:使用空函数指针删除器构造”。它提示的那一行是“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/