应该如何使用std :: optional? [英] How should one use std::optional?
问题描述
我正在阅读 std :: experimental :: optional $的文档c $ c>
,并且我对它的功能有一个好的想法,但是我不明白何时应该使用它或如何使用它。网站不包含任何例子,因为它让我更难掌握这个对象的真正概念。当 std :: optional
是一个好的选择使用,以及如何补偿在以前的标准(C ++ 11)中没有找到的
我可以想到的最简单的例子:
std :: optional< int> try_parse_int(std :: string s)
{
//尝试从给定字符串中解析一个int,
//如果失败则返回nothing
}
使用引用参数(如下面的签名)可以完成同样的事情,但使用 std :: optional
使签名和用法更好。
bool try_parse_int :: string s,int& i);
另一种可以做到的方法是特别糟糕:
int * try_parse_int(std :: string s); // return nullptr if fail
这需要动态内存分配,担心所有权等。
b b pre>
class Contact
{
std :: optional< std :: string>家庭电话;
std ::可选< std :: string>工作电话;
std :: optional< std :: string>移动电话;
};
这是非常喜欢的,而是有一个像 std :: unique_ptr< std :: string>
每个电话号码! std :: optional
为您提供数据本地化,这对于性能非常有用。
另一个例子:
template< typename Key,typename Value>
class Lookup
{
std :: optional< Value> get(Key key);
};
如果查找中没有某个键,那么我们可以简单地返回no value 。
我可以这样使用:
Lookup< std :: string,std :: string> location_lookup;
std :: string location = location_lookup.get(waldo)。value_or(unknown);
另一个例子:
std :: vector< std :: pair< std :: string,double>> search(
std :: string query,
std :: optional< int> max_count,
std :: optional< double> min_match_score);
这比使用四个函数重载更有意义, code> max_count (或不)和 min_match_score
(或不是)!
也消除 已准确通过 -1
为 max_count
min_match_score <$ c>如果您不想要限制或传递
std :: numeric_limits< double& code>如果你不想要最低分数!
b
$ b
std :: optional< int> find_in_string(std :: string s,std :: string query);
如果查询字符串不在 s
,我想没有 int
- 不某人决定为此目的使用任何特殊值(-1?)。
对于其他示例,您可以查看 boost :: optional
< a href =http://www.boost.org/doc/libs/release/libs/optional/doc/html/index.html>文档。 boost :: optional
和 std :: optional
在行为和使用方面基本相同。
I'm reading the documentation of std::experimental::optional
and I have a good idea about what it does, but I don't understand when I should use it or how I should use it. The site doesn't contain any examples as of yet which leaves it harder for me to grasp the true concept of this object. When is std::optional
a good choice to use, and how does it compensate for what was not found in the previous Standard (C++11).
The simplest example I can think of:
std::optional<int> try_parse_int(std::string s)
{
//try to parse an int from the given string,
//and return "nothing" if you fail
}
The same thing might be accomplished with a reference argument instead (as in the following signature), but using std::optional
makes the signature and usage nicer.
bool try_parse_int(std::string s, int& i);
Another way that this could be done is especially bad:
int* try_parse_int(std::string s); //return nullptr if fail
This requires dynamic memory allocation, worrying about ownership, etc. - always prefer one of the other two signatures above.
Another example:
class Contact
{
std::optional<std::string> home_phone;
std::optional<std::string> work_phone;
std::optional<std::string> mobile_phone;
};
This is extremely preferable to instead having something like a std::unique_ptr<std::string>
for each phone number! std::optional
gives you data locality, which is great for performance.
Another example:
template<typename Key, typename Value>
class Lookup
{
std::optional<Value> get(Key key);
};
If the lookup doesn't have a certain key in it, then we can simply return "no value."
I can use it like this:
Lookup<std::string, std::string> location_lookup;
std::string location = location_lookup.get("waldo").value_or("unknown");
Another example:
std::vector<std::pair<std::string, double>> search(
std::string query,
std::optional<int> max_count,
std::optional<double> min_match_score);
This makes a lot more sense than, say, having four function overloads that take every possible combination of max_count
(or not) and min_match_score
(or not)!
It also eliminates the accursed "Pass -1
for max_count
if you don't want a limit" or "Pass std::numeric_limits<double>::min()
for min_match_score
if you don't want a minimum score"!
Another example:
std::optional<int> find_in_string(std::string s, std::string query);
If the query string isn't in s
, I want "no int
" -- not whatever special value someone decided to use for this purpose (-1?).
For additional examples, you could look at the boost::optional
documentation. boost::optional
and std::optional
will basically be identical in terms of behavior and usage.
这篇关于应该如何使用std :: optional?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!