[Docs] Clarify boolean conversion for Error and Expected<T> in the Programmer's

Manual.

llvm-svn: 264135
This commit is contained in:
Lang Hames 2016-03-23 03:18:16 +00:00
parent 7cd8e65187
commit a0f517fc15
1 changed files with 16 additions and 4 deletions

View File

@ -356,12 +356,24 @@ that inherits from the ErrorInfo utility:
return Error::success(); 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>`` 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 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 ``T``, or a ``Error``. Expected<T> values are also implicitly convertible to
for success, false for error. If success, the ``T`` value can be accessed via boolean, but with the opposite convention to Error: true for success, false for
the dereference operator. If failure, the ``Error`` value can be extracted error. If success, the ``T`` value can be accessed via the dereference operator.
using the ``takeError()`` method: If failure, the ``Error`` value can be extracted using the ``takeError()``
method. Idiomatic usage looks like:
.. code-block:: c++ .. code-block:: c++