Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Wrapper to pass shared_ptr to &, const &, *, const * through std::bind.
#include <iostream> #include <functional> #include <memory> using namespace std; struct A { int v = 8; }; void a_ref(A &a) { std::cout << a.v << std::endl; } void a_const_ref(const A &a) { std::cout << a.v << std::endl; } void a_ptr(A *a) { std::cout << a->v << std::endl; } void a_const_ptr(const A *a) { std::cout << a->v << std::endl; } template <typename T> struct shared_ref { shared_ref(const std::shared_ptr<T> &r_) : r(r_) {} shared_ref(const std::shared_ptr<T> &&r_) : r(std::move(r_)) {} operator T&() const { return *r; } operator T*() const { return r.get(); } std::shared_ptr<T> r; }; int main() { auto s = std::make_shared<A>(); std::bind(a_ref, shared_ref<A>(s))(); std::bind(a_const_ref, shared_ref<A>(s))(); std::bind(a_ptr, shared_ref<A>(s))(); std::bind(a_const_ptr, shared_ref<A>(s))(); }
run
|
edit
|
history
|
help
0
hw1 Os
DESim Example Starter Code
Dynamically allocated array in unique_ptr with custom deleter.
applidiumResto_corrigé
Linker error while passing constexpr variable as const &
Tree Example
Merge Sort
Optional conversions
34534
SimpleList Example