C++ std::unique_ptr 从函数返回并测试是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30293338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ std::unique_ptr return from function and test for null
提问by Mendes
I have a function that needs to return a pointer to an object of class myClass
. For this purpose I′m using std::unique_ptr
.
我有一个函数需要返回一个指向 class 对象的指针myClass
。为此,我正在使用std::unique_ptr
.
If the function succeeds, it shall return a pointer to a object with data. If it fails, it should return null
.
如果函数成功,它将返回一个指向带有数据的对象的指针。如果失败,它应该返回null
。
This is my code skeleton:
这是我的代码框架:
std::unique_ptr<myClass> getData()
{
if (dataExists)
... create a new myClass object, populate and return it ...
// No data found
return std::unique_ptr<myClass> (null); // <--- Possible?
}
on main
:
在main
:
main()
{
std::unique_ptr<myClass> returnedData;
returnedData = getData();
if (returnedData != null) // <-- How to test for null?
{
cout << "No data returned." << endl;
return 0;
}
// Process data
}
So here goes my questions:
所以这是我的问题:
a) Is that (returning an object or null
) possible to be done using std::unique_ptr
?
a) 是否可以使用(返回一个对象或null
)来完成std::unique_ptr
?
b) If possible, how to implement is?
b) 如果可能,如何实施?
c) If not possible, what are there alternatives?
c) 如果不可能,有什么替代方案?
Thanks for helping.
谢谢你的帮助。
回答by R Sahu
Either of the following should work:
以下任何一项都应该有效:
return std::unique_ptr<myClass>{};
return std::unique_ptr<myClass>(nullptr);
To test whether the returned object points to a valid object or not, simply use:
要测试返回的对象是否指向有效对象,只需使用:
if ( returnedData )
{
// ...
}
See http://en.cppreference.com/w/cpp/memory/unique_ptr/operator_bool.
请参阅http://en.cppreference.com/w/cpp/memory/unique_ptr/operator_bool。
回答by Barry
Yes it's possible. A default constructed unique_ptr
is what you want:
是的,这是可能的。构造的默认值unique_ptr
是您想要的:
Constructs a
std::unique_ptr
that owns nothing.
构造一个
std::unique_ptr
什么都不拥有的。
// No data found
return std::unique_ptr<myClass>{};
That is equivalent to the nullptr_t
constructor, so perhaps this is more clear:
这相当于nullptr_t
构造函数,所以也许这更清楚:
// No data found
return nullptr;
回答by Chris Drew
Yes, it is possible. A default constructed unique_ptr
or one constructed from nullptr
can be considered null:
对的,这是可能的。一个默认构造unique_ptr
或一个构造自nullptr
可以被认为是空的:
std::unique_ptr<MyClass> getData()
{
if (dataExists)
return std::make_unique<MyClass>();
return nullptr;
}
To test for null either compare against nullptr
or take advantage of conversion to bool:
要测试 null 要么比较要么nullptr
利用转换为 bool:
int main()
{
std::unique_ptr<MyClass> returnedData = getData();
if (returnedData)
{
...
}
}
回答by cardiff space man
In the latest C++ library there should be a make_unique
function in <memory>
allowing us to make unique_ptr
's as easily as in the C++11 library allows with make_shared
and shared pointers.
在最新的 C++ 库中,应该有一个make_unique
函数<memory>
可以让我们unique_ptr
像在 C++11 库中允许的那样轻松地使用make_shared
和共享指针。
So you could elucidate the code a little by returning std::make_unique(nullptr)
所以你可以通过返回来稍微阐明代码 std::make_unique(nullptr)
Plus in the next version of C++ there will be an "option" type which will evaluate as either a some
value or a none
value. The none
value won't be allowed to be treated as if it was a valid value, unlike the empty unique_ptr could be treated like a nullptr. The option type will represent yet another piece of Boost entering the standard library.
另外,在 C++ 的下一版本中,将有一个“选项”类型,它将评估为一个some
值或一个none
值。none
不允许将该值视为有效值,这与空的 unique_ptr 可以视为 nullptr 不同。选项类型将代表另一个进入标准库的 Boost。