Skip to content

Decide whether RAII wrappers are implicitly convertible to pointers to the wrapped type #2

Open
@szhorvat

Description

@szhorvat

Currently, wrappers are convertible to pointers to the wrapped type. An igRealVec can be passed directly where an igraph_vector_t * is expected. This is very convenient, but may have difficult to predict consequences. One particular pitfall that came up was the push_back() member function of typed lists, which needs to behave differently when passed a wrapper of a raw igraph_vector_t *.

// List takes ownership of t
void push_back(value_type &t) {
igCheck(FUNCTION(push_back)(ptr, t));
t.ptr = FUNCTION(tail_ptr)(ptr); // set as alias
}
// List takes ownership of t
void push_back(value_type &&t) {
igCheck(FUNCTION(push_back)(ptr, t));
t.ptr = nullptr;
}
// List takes ownership of t
void push_back(value_type::igraph_type *t) {
igCheck(FUNCTION(push_back)(ptr, t));
}

This is because it must transfer ownership from the igVec to the list, which is impossible once the igVec was converted to an igraph_vector_t *. This was solvable by also providing push_back(igVec &&), but it's easy to not notice this situation and make a mistake.


An alternative to implicit conversion is to overload the & operator, so that instead of

igRealVec v;
igraph_vector_scale(v, 2);

we would need to write

igRealVec v;
igraph_vector_scale(&v, 2);

This is still very concise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions