Skip to content
This repository was archived by the owner on Jan 6, 2022. It is now read-only.
This repository was archived by the owner on Jan 6, 2022. It is now read-only.

Trying to put together minimal C++ usage example #4

Description

@nordlow

Is

#include <iostream>

using std::cout;
using std::endl;

class S
{
public:
    S(int x_) : x(x_) {}
    ~S()
    {
        cout << __PRETTY_FUNCTION__ << ":" << endl;
    }
public:
    int x;
    mutable unsigned int _rc = 1;
};

struct S_traits final
{
    static void increment (S* x) noexcept
    {
        cout << __PRETTY_FUNCTION__ << ":" << endl;
        x->_rc += 1;
    }
    static void decrement (S* x) noexcept
    {
        x->_rc -= 1;
        cout << __PRETTY_FUNCTION__ << ": rc=" << x->_rc << endl;
        if (x->_rc == 0) { delete x; }
    }
};

#include "sg14/memory.hpp"
using SP = sg14::retain_ptr<S, S_traits>;

int main(__attribute__((unused)) int argc,
         __attribute__((unused)) const char * argv[],
         __attribute__((unused)) const char * envp[])
{
    cout << "sizeof(S): " << sizeof(S) << endl;
    cout << "sizeof(SP): " << sizeof(SP) << endl;
    {
        auto rp = SP(new S(42));
    }
    return 0;
}

which prints

sizeof(S): 8
sizeof(SP): 8
static void S_traits::decrement(S*): rc=0
S::~S():

a correct usage?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions