Outils pour utilisateurs

Outils du site


lang:cpp:optional

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

lang:cpp:optional [2023/02/01 12:01] – Création avec "{} ou std::nullopt" rootlang:cpp:optional [2023/03/14 12:27] (Version actuelle) – Ajout des spécificités root
Ligne 1: Ligne 1:
-==={} ou std::nullopt===+===={} ou std::nullopt====
  
 C'est la même chose. C'est la même chose.
Ligne 28: Ligne 28:
  
 [[https://stackoverflow.com/questions/57964217/stdoptional-construct-empty-with-or-stdnullopt|std::optional - construct empty with {} or std::nullopt?]] {{ :lang:cpp:optional:c_-_std_optional_-_construct_empty_with_or_std_nullopt_-_stack_overflow_01_02_2023_11_59_46_.html |Archive du 16/09/2019 le 01/02/2023}} [[https://stackoverflow.com/questions/57964217/stdoptional-construct-empty-with-or-stdnullopt|std::optional - construct empty with {} or std::nullopt?]] {{ :lang:cpp:optional:c_-_std_optional_-_construct_empty_with_or_std_nullopt_-_stack_overflow_01_02_2023_11_59_46_.html |Archive du 16/09/2019 le 01/02/2023}}
 +
 +====std::make_optional====
 +''std::make_optional'' force la création d'un optional alors que std::optional peut copier un optional.
 +
 +<code cpp>
 +#include <optional>
 +#include <type_traits>
 +
 +int main()
 +{
 +    auto inner=std::make_optional(325);
 +    auto opt2=std::make_optional(inner); // makes std::optional<std::optional<int>>
 +    auto opt3=std::optional(inner);      // just a copy of inner
 +    static_assert(std::is_same_v<decltype(opt2), std::optional<std::optional<int>>>);
 +    static_assert(std::is_same_v<decltype(opt3), std::optional<int>>);
 +}
 +</code>
 +
 +[[https://stackoverflow.com/questions/62734897/what-is-the-point-of-stdmake-optional|What is the point of `std::make_optional`]] {{ :lang:cpp:optional:c_-_what_is_the_point_of_std_make_optional_-_stack_overflow_14_03_2023_12_26_26_.html |Archive du 04/07/2020 le 14/03/2023}}
 +
 +====Spécificités====
 +===Opérateur ternaire===
 +
 +Il faut spécifier explicitement l'optional.
 +
 +<code cpp>
 +std::optional<int> t;
 +t = true ? 1 : std::optional<int>{std::nullopt};
 +</code>
 +
lang/cpp/optional.1675249286.txt.gz · Dernière modification : 2023/02/01 12:01 de root