MCPcopy Create free account
hub / github.com/pybind/pybind11 / try_get_shared_from_this

Function try_get_shared_from_this

include/pybind11/detail/common.h:1354–1369  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1352
1353template <typename T>
1354inline static std::shared_ptr<T>
1355try_get_shared_from_this(std::enable_shared_from_this<T> *holder_value_ptr) {
1356// Pre C++17, this code path exploits undefined behavior, but is known to work on many platforms.
1357// Use at your own risk!
1358// See also https://en.cppreference.com/w/cpp/memory/enable_shared_from_this, and in particular
1359// the `std::shared_ptr<Good> gp1 = not_so_good.getptr();` and `try`-`catch` parts of the example.
1360#if defined(__cpp_lib_enable_shared_from_this) && (!defined(_MSC_VER) || _MSC_VER >= 1912)
1361 return holder_value_ptr->weak_from_this().lock();
1362#else
1363 try {
1364 return holder_value_ptr->shared_from_this();
1365 } catch (const std::bad_weak_ptr &) {
1366 return nullptr;
1367 }
1368#endif
1369}
1370
1371// For silencing "unused" compiler warnings in special situations.
1372template <typename... Args>

Callers 3

init_holderMethod · 0.85
set_via_shared_from_thisFunction · 0.85

Calls 1

lockMethod · 0.80

Tested by

no test coverage detected