#shared_ptr

Uli Kusterer (Not a kitteh)uliwitness@chaos.social
2025-04-02

I wonder if it'd work to add a guaranteed non-null pointer to C++ like this:

gist.github.com/uliwitness/931

#CPlusPlus #optionals #shared_ptr

Update: The gist is now fully compiling code.

/*  Since shared_ptr may always be nullptr, it already is an optional.
    But that means we need a non-nullable version of shared_ptr.
    
    Prerequisites: 1. Can't be assigned a null value
                   2. Can be assigned a non-null
                   3. Can be copied to a shared_ptr or another nonoptional_shared_ptr
    
    Would probably also need nonoptional_shared_ptr-returning versions of make_shared<T> etc.
*/

using namespace std;

template<class T>
class nonoptional_shared_ptr {
public:
    nonoptional_shared_ptr(const nonoptional_shared_ptr<T>& p) : _ptr(p._ptr) {}
    nonoptional_shared_ptr<T>& operator= (const nonoptional_shared_ptr<T>& p) { _ptr = p._ptr; }

    operator shared_ptr<T> () { return _ptr; }
    
    T* get() { return _ptr.get(); }
    T* operator -> () { return _ptr.get(); }
    T& operator * () { return *_ptr; }

    // Add more methods here that forward shared_ptr methods so it can be used same way.
  
protected:
    nonoptional_shared_ptr(const shared_ptr<T>& p) : _ptr(p) {}
    shared_ptr<T> _ptr;
  
    friend void if_nonnullptr(shared_ptr<T> possiblyNull, function<void(nonoptional_shared_ptr<T>)> nonnullHandler, function<void()> nullHandler);
};

void if_nonnullptr(shared_ptr<T> possiblyNull, function<void(nonoptional_shared_ptr<T>)> nonnullHandler, function<void()> nullHandler) {
    if (possiblyNull) {
        nonnullHandler(nonoptional_shared_ptr(possiblyNull));
    } else {
        nullHandler();
    }
}class Foo {
public:
  
    Foo(const nonoptional_shared_ptr& p) : _neverNull(p) {}
  
protected:
    nonoptional_shared_ptr _neverNull;
};

int main(int argc, const char** argv) {
    shared_ptr<Bar> bar = make_shared<Bar>();
    shared_ptr<Foo> dest;
  
    if_nonnullptr(bar,
                  [&dest](const nonoptional_shared_ptr& p) {
                      dest = make_shared<Foo>(p);
                  },
                  [](){
                      cerr << "Error!" << endl;
                  });
    
    // Now we have an (optional) Foo, but if it exists, we know that its _neverNull is a valid pointer.
    
    return EXIT_SUCCESS;
}
2024-10-18

TIL that std::shared_ptr has a custom "deleter" function you can set for actually deleting the memory. Seems to make for a pretty nice way to create a memory buffer manager. I need to shuffle a lot of smallish buffers and allocating/freeing memory all the time is not particularly efficient, especially not when time is essential. I guess this is nothing new to the veterans out there, but it'll make my life easier.

#cpp #std #shared_ptr

2018-05-13

weak_from_thisか・・・なるほど。
enable_shared_from_this の C++2011(14) と C++2017 との相違点 by yuki12 qiita.com/yuki12/items/ccbe5cf #Qiita #Cpp #Cpp11 #Cpp17 #shared_ptr

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst