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
{{ message }}
Repository navigation
Commit a8fedfa
Browse filesBrowse the repository at this point in the historyBrowse files
Support returning std::expected and custom expected types from embind
functions, methods, and constructors.
Functions returning an expected type unwrap the value on success or
throw the contained error as a JavaScript exception on failure.
Fixes#28112
Copy file name to clipboardExpand all lines: site/source/docs/porting/connecting_cpp_and_javascript/embind.rst
+28-3Lines changed: 28 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1167,14 +1167,16 @@ Out of the box, *embind* provides converters for many standard C++ types:
1167
1167
1168
1168
For convenience, *embind* provides factory functions to register
1169
1169
``std::vector<T, class Allocator=std::allocator<T>>`` (:cpp:func:`register_vector`), ``std::map<K, V, class Compare=std::less<K>, class Allocator=std::allocator<std::pair<const K, V>>>``
1170
-
(:cpp:func:`register_map`), and ``std::optional<T>`` (:cpp:func:`register_optional`) types:
1170
+
(:cpp:func:`register_map`), ``std::optional<T>`` (:cpp:func:`register_optional`), and ``std::expected<T, E>`` (:cpp:func:`register_expected`) types:
1171
1171
1172
1172
.. code:: cpp
1173
1173
1174
1174
EMSCRIPTEN_BINDINGS(stl_wrappers) {
1175
1175
register_vector<int>("VectorInt");
1176
1176
register_map<int,int>("MapIntInt");
1177
1177
register_optional<std::string>();
1178
+
register_expected<int>(); // defaults error type to emscripten::val
1179
+
register_expected<int, std::string>();
1178
1180
}
1179
1181
1180
1182
A full example is shown below:
@@ -1185,6 +1187,7 @@ A full example is shown below:
1185
1187
#include <string>
1186
1188
#include <vector>
1187
1189
#include <optional>
1190
+
#include <expected>
1188
1191
1189
1192
using namespace emscripten;
1190
1193
@@ -1203,16 +1206,25 @@ A full example is shown below:
0 commit comments