| OLD | NEW |
| 1 # base::Optional | 1 # base::Optional |
| 2 | 2 |
| 3 `base::Optional<T>` is a container that might contain an instance of `T`. | 3 `base::Optional<T>` is a container that might contain an instance of `T`. |
| 4 | 4 |
| 5 [TOC] | 5 [TOC] |
| 6 | 6 |
| 7 ## History | 7 ## History |
| 8 | 8 |
| 9 [base::Optional<T>](https://code.google.com/p/chromium/codesearch#chromium/src/b
ase/optional.h) | 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), | 10 is an implementation of [std::optional<T>](http://en.cppreference.com/w/cpp/util
ity/optional), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 base::Optional<int> opt_2 = 2; | 56 base::Optional<int> opt_2 = 2; |
| 57 | 57 |
| 58 opt_1 == opt_2; // false | 58 opt_1 == opt_2; // false |
| 59 opt_1 = 1; | 59 opt_1 = 1; |
| 60 | 60 |
| 61 opt_1 <= opt_2; // true | 61 opt_1 <= opt_2; // true |
| 62 opt_1 == 1; // true | 62 opt_1 == 1; // true |
| 63 opt_1 == base::nullopt_t; // false | 63 opt_1 == base::nullopt_t; // false |
| 64 ``` | 64 ``` |
| 65 | 65 |
| 66 `base::Optional<T>` has a helper function `make_optional<T&&>`: | 66 `base::Optional<T>` has a helper function `base::make_optional<T&&>`: |
| 67 | 67 |
| 68 ```C++ | 68 ```C++ |
| 69 base::Optional<int> opt = make_optional<int>(GetMagicNumber()); | 69 base::Optional<int> opt = base::make_optional<int>(GetMagicNumber()); |
| 70 ``` | 70 ``` |
| 71 | 71 |
| 72 Finally, `base::Optional<T>` is integrated with `std::hash`, using | 72 Finally, `base::Optional<T>` is integrated with `std::hash`, using |
| 73 `std::hash<T>` if it is not empty, a default value otherwise. `.emplace()` and | 73 `std::hash<T>` if it is not empty, a default value otherwise. `.emplace()` and |
| 74 `.swap()` can be used as members functions and `std::swap()` will work with two | 74 `.swap()` can be used as members functions and `std::swap()` will work with two |
| 75 `base::Optional<T>` objects. | 75 `base::Optional<T>` objects. |
| 76 | 76 |
| 77 ## How is it implemented? | 77 ## How is it implemented? |
| 78 | 78 |
| 79 `base::Optional<T>` is implemented with a union with a `T` member. The object | 79 `base::Optional<T>` is implemented with a union with a `T` member. The object |
| (...skipping 19 matching lines...) Expand all Loading... |
| 99 | 99 |
| 100 ## When not to use? | 100 ## When not to use? |
| 101 | 101 |
| 102 It is recommended to not use `base::Optional<T>` as a function parameter as it | 102 It is recommended to not use `base::Optional<T>` as a function parameter as it |
| 103 will force the callers to use `base::Optional<T>`. Instead, it is recommended to | 103 will force the callers to use `base::Optional<T>`. Instead, it is recommended to |
| 104 keep using `T*` for arguments that can be omitted, with `nullptr` representing | 104 keep using `T*` for arguments that can be omitted, with `nullptr` representing |
| 105 no value. | 105 no value. |
| 106 | 106 |
| 107 Furthermore, depending on `T`, MSVC might fail to compile code using | 107 Furthermore, depending on `T`, MSVC might fail to compile code using |
| 108 `base::Optional<T>` as a parameter because of memory alignment issues. | 108 `base::Optional<T>` as a parameter because of memory alignment issues. |
| OLD | NEW |