COMS W4995 C++ Deep Dive for C Programmers

Index of 2026-9/code/20

Parent directory
Makefile
shared-ptr-with-deleter.cpp
smart-ptr-deleters.cpp

Makefile

CC  = g++
CXX = g++

CFLAGS   = -g -Wall
CXXFLAGS = -g -Wall -std=c++20

executables = smart-ptr-deleters shared-ptr-with-deleter

.PHONY: default
default: $(executables)

smart-ptr-deleters: smart-ptr-deleters.o

shared-ptr-with-deleter: shared-ptr-with-deleter.o

.PHONY: clean
clean:
	rm -f *~ a.out core *.o $(executables)

.PHONY: all
all: clean default

shared-ptr-with-deleter.cpp

#include <iostream>
#include <memory>
#include <utility>

using namespace std;

template <typename T>
class Deleter {
public:
    template <typename F>
    Deleter(F f) : impl{new Model<F>{std::move(f)}} {}
    void operator()(T* p) { (*impl)(p); }

    // Copy and move support, which requires clone():
    Deleter(const Deleter& other) : impl{other.impl->clone()} {}
    Deleter& operator=(const Deleter& other) {
        impl = other.impl->clone();
        return *this;
    }
    Deleter(Deleter&&) = default;
    Deleter& operator=(Deleter&&) = default;
private:
    struct Concept {
        virtual void operator()(T*) = 0;
        virtual unique_ptr<Concept> clone() const = 0;
        virtual ~Concept() = default;
    };
    template <typename F>
    struct Model : Concept {
        F f;
        Model(F f) : f{std::move(f)} {}
        unique_ptr<Concept> clone() const override {
            return make_unique<Model<F>>(f);
        }
        void operator()(T* p) override { f(p); }
    };

    unique_ptr<Concept> impl;
};

namespace sp_v1 {

template <typename T>
class SharedPtr {
    T*         ptr;    // the underlying pointer
    int*       count;  // the reference count
    Deleter<T> del;    // each SharedPtr holds its own copy of the deleter

    void detach() {
        if (--*count == 0) {
            delete count;
            del(ptr);
        }
    }
public:
    explicit SharedPtr(T* p) : SharedPtr(p, default_delete<T>{}) {}
    template <typename F>
    SharedPtr(T* p, F f) : ptr{p}, count{new int{1}}, del{std::move(f)} {}
    ~SharedPtr() { detach(); }

    SharedPtr(const SharedPtr& sp) : ptr{sp.ptr}, count{sp.count}, del{sp.del} {
        ++*count;
    }
    SharedPtr& operator=(const SharedPtr& sp) {
        if (this != &sp) {
            detach();
            ptr = sp.ptr;
            count = sp.count;
            del = sp.del;
            ++*count;
        }
        return *this;
    }

    T& operator*() const { return *ptr; }
    T* operator->() const { return ptr; }
    int get_count() const { return *count; }
};

}  // namespace sp_v1

namespace sp_v2 {

template <typename T>
class SharedPtr {
    struct ControlBlock {
        int count = 1;
        virtual void destroy(T* p) = 0;
        virtual ~ControlBlock() = default;
    };
    template <typename F>
    struct Model : ControlBlock {
        F del;
        Model(F f) : del{std::move(f)} {}
        void destroy(T* p) override { del(p); }
    };

    T* ptr;
    ControlBlock* cb;

    void detach() {
        if (--cb->count == 0) {
            cb->destroy(ptr);
            delete cb;
        }
    }
public:
    explicit SharedPtr(T* p) : SharedPtr(p, default_delete<T>{}) {}
    template <typename F>
    SharedPtr(T* p, F f) : ptr{p}, cb{new Model<F>{std::move(f)}} {}
    ~SharedPtr() { detach(); }

    SharedPtr(const SharedPtr& sp) : ptr{sp.ptr}, cb{sp.cb} { ++cb->count; }
    SharedPtr& operator=(const SharedPtr& sp) {
        if (this != &sp) {
            detach();
            ptr = sp.ptr;
            cb = sp.cb;
            ++cb->count;
        }
        return *this;
    }

    T& operator*() const { return *ptr; }
    T* operator->() const { return ptr; }
    int get_count() const { return cb->count; }
};

}  // namespace sp_v2

struct DelFunctor {
    void operator()(int* p) const {
        cout << "DelFunctor\n";
        delete p;
    }
};

int main() {
    auto del_lambda = [](int* p) {
        cout << "del_lambda\n";
        delete p;
    };

    cout << "shared_ptr deleters:\n";
    {
        // Different functor types, same Deleter<int> type
        Deleter<int> d1{DelFunctor{}};
        Deleter<int> d2{del_lambda};

        // Deleters can delete int* pointers
        d1(new int(1));
        d2(new int(2));

        // OK: Deleter<int> is movable
        d1 = std::move(d2);
        // Now calls DelFunctor
        d1(new int(3));

        // Requires implementing copy for Deleter
        Deleter<int> d3 = d1;
    }

    cout << "SharedPtr test:\n";
    {
        sp_v2::SharedPtr<int> s1(new int(1), DelFunctor{});
        sp_v2::SharedPtr<int> s2(new int(2), del_lambda);
        s1 = s2;  // s1's old int is freed with DelFunctor
        cout << "count: " << s1.get_count() << "\n";
    }
}

smart-ptr-deleters.cpp

#include <iostream>
#include <memory>

struct DelFunctor {
    void operator()(int* p) const {
        delete p;
    }
};

int main() {
    auto del_lambda = [](int* p) {
        delete p;
    };

    // unique_ptr: deleter is part of the type
    std::unique_ptr<int, DelFunctor> u1(new int(1));
    std::unique_ptr<int, decltype(del_lambda)> u2(new int(2), del_lambda);

    // error: u1 and u2 have different types!
    // u1 = std::move(u2);

    std::unique_ptr<int, DelFunctor> u3(new int(3));
    // OK: both are unique_ptr<int, DelFunctor>
    u1 = std::move(u3);

    // shared_ptr: deleter is not part of the type
    std::shared_ptr<int> s1(new int(1), DelFunctor{});
    std::shared_ptr<int> s2(new int(2), del_lambda);
    // OK: both are shared_ptr<int>
    s1 = s2;
}