@@ -1875,6 +1875,21 @@ class class_ {
18751875 }
18761876};
18771877
1878+ #if __cplusplus >= 201703L
1879+ template <typename T>
1880+ void register_optional () {
1881+ // Optional types are automatically registered for some internal types so
1882+ // only run the register method once so we don't conflict with a user's
1883+ // bindings if they also register the optional type.
1884+ thread_local bool hasRun;
1885+ if (hasRun) {
1886+ return ;
1887+ }
1888+ hasRun = true ;
1889+ internal::_embind_register_optional (internal::TypeID<std::optional<T>>::get (), internal::TypeID<T>::get ());
1890+ }
1891+ #endif
1892+
18781893// //////////////////////////////////////////////////////////////////////////////
18791894// VECTORS
18801895// //////////////////////////////////////////////////////////////////////////////
@@ -1883,6 +1898,18 @@ namespace internal {
18831898
18841899template <typename VectorType>
18851900struct VectorAccess {
1901+ #if __cplusplus >= 201703L
1902+ static std::optional<typename VectorType::value_type> get (
1903+ const VectorType& v,
1904+ typename VectorType::size_type index
1905+ ) {
1906+ if (index < v.size ()) {
1907+ return v[index];
1908+ } else {
1909+ return {};
1910+ }
1911+ }
1912+ #else
18861913 static val get (
18871914 const VectorType& v,
18881915 typename VectorType::size_type index
@@ -1893,6 +1920,7 @@ struct VectorAccess {
18931920 return val::undefined ();
18941921 }
18951922 }
1923+ #endif
18961924
18971925 static bool set (
18981926 VectorType& v,
@@ -1909,6 +1937,9 @@ struct VectorAccess {
19091937template <typename T>
19101938class_<std::vector<T>> register_vector (const char * name) {
19111939 typedef std::vector<T> VecType;
1940+ #if __cplusplus >= 201703L
1941+ register_optional<T>();
1942+ #endif
19121943
19131944 void (VecType::*push_back)(const T&) = &VecType::push_back;
19141945 void (VecType::*resize)(const size_t , const T&) = &VecType::resize;
@@ -1923,13 +1954,6 @@ class_<std::vector<T>> register_vector(const char* name) {
19231954 ;
19241955}
19251956
1926- #if __cplusplus >= 201703L
1927- template <typename T>
1928- void register_optional () {
1929- internal::_embind_register_optional (internal::TypeID<std::optional<T>>::get (), internal::TypeID<T>::get ());
1930- }
1931- #endif
1932-
19331957// //////////////////////////////////////////////////////////////////////////////
19341958// MAPS
19351959// //////////////////////////////////////////////////////////////////////////////
@@ -1938,6 +1962,19 @@ namespace internal {
19381962
19391963template <typename MapType>
19401964struct MapAccess {
1965+ #if __cplusplus >= 201703L
1966+ static std::optional<typename MapType::mapped_type> get (
1967+ const MapType& m,
1968+ const typename MapType::key_type& k
1969+ ) {
1970+ auto i = m.find (k);
1971+ if (i == m.end ()) {
1972+ return {};
1973+ } else {
1974+ return i->second ;
1975+ }
1976+ }
1977+ #else
19411978 static val get (
19421979 const MapType& m,
19431980 const typename MapType::key_type& k
@@ -1949,6 +1986,7 @@ struct MapAccess {
19491986 return val (i->second );
19501987 }
19511988 }
1989+ #endif
19521990
19531991 static void set (
19541992 MapType& m,
@@ -1975,6 +2013,9 @@ struct MapAccess {
19752013template <typename K, typename V>
19762014class_<std::map<K, V>> register_map (const char * name) {
19772015 typedef std::map<K,V> MapType;
2016+ #if __cplusplus >= 201703L
2017+ register_optional<V>();
2018+ #endif
19782019
19792020 size_t (MapType::*size)() const = &MapType::size;
19802021 return class_<MapType>(name)
0 commit comments