diff options
author | Lang Hames <lhames@gmail.com> | 2016-03-23 03:18:16 +0000 |
---|---|---|
committer | Lang Hames <lhames@gmail.com> | 2016-03-23 03:18:16 +0000 |
commit | 38219923934efac6fa7289deebe6ccde64dfb0bd (patch) | |
tree | 23ad92806530b4df544ae397cfeedd8c20ab559c /docs/ProgrammersManual.rst | |
parent | ab71fdec0a19267c7b809afb7bcbe7465f1099a2 (diff) |
[Docs] Clarify boolean conversion for Error and Expected<T> in the Programmer's
Manual.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@264135 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'docs/ProgrammersManual.rst')
-rw-r--r-- | docs/ProgrammersManual.rst | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/docs/ProgrammersManual.rst b/docs/ProgrammersManual.rst index 1258ad6e8b57..5ad5113bb4b1 100644 --- a/docs/ProgrammersManual.rst +++ b/docs/ProgrammersManual.rst @@ -356,12 +356,24 @@ that inherits from the ErrorInfo utility: return Error::success(); } +Error values can be implicitly converted to bool: true for error, false for +success, enabling the following idiom: + +.. code-block:: + + if (auto Err = mayFail()) + return Err; + + // Success! We can proceed. + + For functions that can fail but need to return a value the ``Expected<T>`` utility can be used. Values of this type can be constructed with either a -``T``, or a ``Error``. Values are implicitly convertible to boolean: true -for success, false for error. If success, the ``T`` value can be accessed via -the dereference operator. If failure, the ``Error`` value can be extracted -using the ``takeError()`` method: +``T``, or a ``Error``. Expected<T> values are also implicitly convertible to +boolean, but with the opposite convention to Error: true for success, false for +error. If success, the ``T`` value can be accessed via the dereference operator. +If failure, the ``Error`` value can be extracted using the ``takeError()`` +method. Idiomatic usage looks like: .. code-block:: c++ |