OLD | NEW |
---|---|
(Empty) | |
1 # base::Optional | |
2 | |
3 `base::Optional<T>` is a container that might contain an instance of `T`. | |
4 | |
5 [TOC] | |
6 | |
7 ## History | |
8 | |
9 [base::Optional<T>](https://code.google.com/p/chromium/codesearch#chromium/src/b ase/optional.h) | |
10 is an implementation of [std::optional<T>](http://en.cppreference.com/w/cpp/util ity/optional), | |
11 initially a C++ experimental feature and now part of the C++17 standard. The Chr omium's | |
12 implementation is as close as possible to the specification. The differences are listed | |
13 at the beginning of the header. The most important difference is that all the ob jects and | |
14 types are part of the `base::` namespace instead of `std::`. Also, following Chr omium coding | |
15 style, the class in named `Optional` instead of `optional`. | |
16 | |
17 ## API description | |
18 | |
19 For a deep API description, please have a look at [std::optional<T>](http://en.c ppreference.com/w/cpp/utility/optional) | |
20 or the [Chromium implementation](https://code.google.com/p/chromium/codesearch#c hromium/src/base/optional.h). | |
21 | |
22 When initialized without a value, `base::Optional<T>` will be empty. When empty, the `operator bool` | |
23 will return `false` and `value()` should not be called. An empty `base::Optional <T>` is equal to | |
24 `base::nullopt_t`. | |
25 ```C++ | |
26 base::Optional<int> opt; | |
27 opt == true; // false | |
28 opt.value(); // illegal, will DCHECK | |
29 opt == base::nullopt_t; // true | |
30 ``` | |
31 | |
32 To avoid calling `value()` when an `base::Optional<T>` is empty, instead of doin g checks, it is | |
33 possible to use `value_or()` if a default value is available: | |
34 ```C++ | |
35 base::Optional<int> opt; | |
36 opt.value_or(42); // will return 42 | |
37 opt == base::nullopt_t; // false | |
38 ``` | |
39 | |
40 It is possible to initialize a `base::Optional<T>` from its constructor and `ope rator=` using | |
41 another `base::Optional<T>`: | |
42 ```C++ | |
43 base::Optional<int> opt_1 = 1; // .value() == 1 | |
44 base::Optional<int> opt_2 = base::Optional<int>(2); // .value() == 2 | |
45 ``` | |
46 | |
47 All basic operators should be available on `base::Optional<T>`: it is possible t o compare a | |
48 `base::Optional<T>` with another or with a `T` or `base::nullopt_t`. | |
49 ```C++ | |
50 base::Optional<int> opt_1; | |
51 base::Optional<int> opt_2 = 2; | |
52 | |
53 opt_1 == opt_2; // false | |
54 opt_1 = 1; | |
55 | |
56 opt_1 <= opt_2; // true | |
57 opt_1 == 1; // true | |
58 ``` | |
59 | |
60 `base::Optional<T>` has a helper function `make_optional<T&&>`: | |
61 ```C++ | |
62 base::Optional<int> opt = make_optional<int>(GetMagicNumber()); | |
63 ``` | |
64 | |
65 Finally, `base::Optional<T>` is integrated with `std::hash`, using `std::hash<T> ` if it is not empty, | |
66 a default value otherwise. `.emplace()` and `.swap()` can be used as members fun ctions and `std::swap()` | |
67 will work with two `base::Optional<T>` objects. | |
68 | |
69 ## How is it implemented? | |
70 | |
71 `base::Optional<T>` is implemented using `base::AlignedMemory`. The object doesn 't behave | |
72 like a pointer and doesn't do dynamic memory allocation. In other words, it is g uaranteed | |
73 to have an object allocated when it is not empty. | |
74 | |
75 ## When to use? | |
76 | |
77 A very common use case is for classes and structures that have an object not alw ays available, | |
78 because it is early initialized or because the underlying data structure doesn't require it. | |
79 It is common to implement such patterns with dynamically allocated pointers, `nu llptr` | |
80 representing the absence of the data. Other approaches involve `std::pair<T, boo l>` where | |
81 bool represents whether the object is actually present. It can also be used for simple types, | |
82 for example when a structure wants to represent whether the user or the underlyi ng data | |
83 structure has some value unspecified, a `base::Optional<int>` would be easier to understand | |
84 than a special value representing the lack of it. | |
85 | |
86 ## When not to use? | |
87 | |
88 TODO: list cases and explain why. | |
danakj
2016/04/14 20:16:20
Talked a bit with jyasskin about this, I think the
mlamouri (slow - plz ping)
2016/04/16 11:43:43
Done. Also mentioned the MSVC issue.
| |
OLD | NEW |