environment. In this case representing the error as a string makes perfect
sense. LLVM provides the ``StringError`` class for this purpose. It takes two
arguments: A string error message, and an equivalent ``std::error_code`` for
-interoperability:
+interoperability. It also provides a ``createStringError`` function to simplify
+common usage of this class:
.. code-block:: c++
- make_error<StringError>("Bad executable",
- make_error_code(errc::executable_format_error"));
+ // These two lines of code are equivalent:
+ make_error<StringError>("Bad executable", errc::executable_format_error);
+ createStringError(errc::executable_format_error, "Bad executable");
If you're certain that the error you're building will never need to be converted
to a ``std::error_code`` you can use the ``inconvertibleErrorCode()`` function:
.. code-block:: c++
- make_error<StringError>("Bad executable", inconvertibleErrorCode());
+ createStringError(inconvertibleErrorCode(), "Bad executable");
This should be done only after careful consideration. If any attempt is made to
convert this error to a ``std::error_code`` it will trigger immediate program
can convert to, and even (as painful as it is) consider introducing a new one as
a stopgap measure.
+``createStringError`` can take ``printf`` style format specifiers to provide a
+formatted message:
+
+.. code-block:: c++
+
+ createStringError(errc::executable_format_error,
+ "Bad executable: %s", FileName);
+
Interoperability with std::error_code and ErrorOr
"""""""""""""""""""""""""""""""""""""""""""""""""