From 143835aea8ab25d4ceb9d1413c16de97f664f961 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Thu, 16 Feb 2023 14:16:47 +0100 Subject: Coding style: Avoid optional::value() Change-Id: Ic4769c6f9f016415e01ca5526f6730bc93c6ea81 Reviewed-by: Jarek Kobus Reviewed-by: Leena Miettinen --- doc/qtcreatordev/src/coding-style.qdoc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'doc/qtcreatordev') diff --git a/doc/qtcreatordev/src/coding-style.qdoc b/doc/qtcreatordev/src/coding-style.qdoc index 02636e3ab5..22da9cca0c 100644 --- a/doc/qtcreatordev/src/coding-style.qdoc +++ b/doc/qtcreatordev/src/coding-style.qdoc @@ -899,6 +899,29 @@ container is const or unshared, use \c{std::cref()} to ensure that the container is not unnecessarily detached. + \section3 std::optional + + Avoid the throwing function \c{value()}. Check the availability of the value first, and then use + the non-throwing functions for accessing values, like \c{operator*} and \c{operator->}. + In very simple cases, you can also use \c{value_or()}. + + \code + + if (optionalThing) { + val = optionalThing->member; + other = doSomething(*optionalThing); + } + + -NOT- + + if (optionalThing) { + val = optionalThing.value().member; + other = doSomething(optionalThing.value()); + } + + \endcode + + \section2 Using QObject \list -- cgit v1.2.1