gpt4 book ai didi

c++ - 带有空指针的 std::make_unique?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:12:26 25 4
gpt4 key购买 nike

我无法找到任何关于如果将空指针传递给 std::make_unique 会发生什么的文档。

是否抛出异常?

最佳答案

std::make_unique将传递给匹配目标构造函数的所有参数转发。因此,如果您传递 nullptr,它将搜索接受 nullptr_t 的构造函数(或任何匹配的构造函数)。如果不存在,将无法编译。

class A
{
public:
A(void*) { ... } // could also be A(int*) or template <typename T> A(T*)
};

...
std::make_unique<A>(nullptr); // constructs an A by calling A(void*), passing nullptr.

关于c++ - 带有空指针的 std::make_unique?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56993669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com