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 is 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 ``` | |
38 | |
39 It is possible to initialize a `base::Optional<T>` from its constructor and `ope rator=` using | |
40 another `base::Optional<T>`: | |
41 ```C++ | |
42 base::Optional<int> opt_1 = 1; // .value() == 1 | |
43 base::Optional<int> opt_2 = base::Optional<int>(2); // .value() == 2 | |
44 ``` | |
45 | |
46 All basic operators should be available on `base::Optional<T>`: it is possible t o compare a | |
47 `base::Optional<T>` with another or with a `T` or `base::nullopt_t`. | |
48 ```C++ | |
49 base::Optional<int> opt_1; | |
50 base::Optional<int> opt_2 = 2; | |
51 | |
52 opt_1 == opt_2; // false | |
53 opt_1 = 1; | |
54 | |
55 opt_1 <= opt_2; // true | |
56 opt_1 == 1; // true | |
57 opt_1 == base::nullopt_t; // false | |
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 | |
80 It is common to implement such patterns with dynamically allocated pointers, `nu llptr` | |
81 representing the absence of value. Other approaches involve `std::pair<T, bool>` where | |
82 bool represents whether the object is actually present. | |
83 | |
84 It can also be used for simple types, | |
85 for example when a structure wants to represent whether the user or the underlyi ng data | |
86 structure has some value unspecified, a `base::Optional<int>` would be easier to understand | |
87 than a special value representing the lack of it. | |
88 | |
89 ## When not to use? | |
90 | |
91 It is recommended to not use `base::Optional<T>` as a function parameter as it w ill force the | |
danakj
2016/04/18 21:08:10
FWIW, I'm not sure what line length you used but 8
| |
92 callers to use `base::Optional<T>`. Instead, it is recommended to keep using `T* ` for arguments that | |
93 can be ommited, with `nullptr` representing no value. | |
94 | |
95 Furthermore, MSVC might fail to compile | |
96 code using `base::Optional<T>` as a parameter because of memory alignment issues . | |
OLD | NEW |