You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/leaf.adoc
+10-7Lines changed: 10 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -861,7 +861,7 @@ Besides error objects, `load` can take function arguments:
861
861
862
862
* If we pass a function that takes no arguments, it is invoked, and the returned error object is loaded.
863
863
+
864
-
Consider that if we pass to `load` an error object that is not used by an error handler, it will be discarded. If the object is expensive to compute, it would be better if the computation is only performed in case of an error. Passing a function with no arguments to `load` is an excellent way to achieve this behavior:
864
+
Consider that if we pass to `load` an error object that is not used by an error handler, it will be discarded. If instead of an error object we pass a function that returns an error object, that function will only be called if the object it returns is needed, that is, if it will not be discarded. This is helpful when the error object is relatively expensive to produce:
865
865
+
866
866
[source,c++]
867
867
----
@@ -968,7 +968,7 @@ When we invoke `on_error`, we can pass three kinds of arguments:
968
968
. Functions that take no arguments and return an error object;
969
969
. Functions that take a single error object by mutable reference.
970
970
971
-
For example, if we want to use `on_error` to capture `errno`, we can't just pass <<e_errno>> to it, because at that time it hasn't been set (yet). Instead, we'd pass a function that returns it:
971
+
For example, if we want to use `on_error` to capture `errno`, we could use the <<e_errno>> type, which is a simple struct that wraps an `int`. But, we can't just pass an <<e_errno>> to `on_error`, because at that time `errno` hasn't been set (yet). Instead, we'd pass a function that returns it:
972
972
973
973
[source,c++]
974
974
----
@@ -1821,12 +1821,15 @@ The `diagnostic_details` output for the snippet above tells us that we got an `e
1821
1821
1822
1822
----
1823
1823
Unrecognized error detected
1824
-
Error serial #1
1825
-
Diagnostic details:
1824
+
Error with serial #1
1825
+
Caught:
1826
1826
error_code: 1
1827
+
Diagnostic details:
1827
1828
boost::leaf::e_file_name: file.txt
1828
1829
----
1829
1830
1831
+
TIP: In the `diagnostic_details` output, the section under `Caught:` lists the objects which error handlers take as arguments -- these are the objects which are stored on the stack. The section under `Diagnostic details:` lists all other objects that were communicated. These are the objects that would have been discarded if we didn't provide a handler that takes `diagnostic_details`.
1832
+
1830
1833
To print each error object, LEAF attempts to bind an unqualified call to `operator<<`, passing a `std::ostream` and the error object. If that fails, it will also attempt to bind `operator<<` that takes the `.value` of the error type. If that also does not compile, the error object value will not appear in diagnostic messages, though LEAF will still print its type.
1831
1834
1832
1835
Even with error types that define a printable `.value`, the user may still want to overload `operator<<` for the enclosing `struct`, e.g.:
Notice how the diagnostic information for `e_file_name` changed: because it was discarded, LEAF is unable to print it.
1886
+
Notice how we are missing the `Diagnostic details:` section. That's because the `e_file_name` object was discarded by LEAF, since no error handler needed it.
1884
1887
1885
-
TIP: The automatically-generated diagnostic messages are developer-friendly, but not user-friendly. Therefore, `operator<<` overloads for error types should only print technical information in English, and should not attempt to localize strings or to format a user-friendly message; this should be done in error handling functions specifically designed for that purpose.
1888
+
TIP: The automatically-generated diagnostic messages are developer-friendly, but not user-friendly.
0 commit comments