Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
szhorvat opened this issue Jul 12, 2024 · 0 comments
Milestone

Comments

@szhorvat
Copy link
Member

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.

@szhorvat szhorvat added this to the 0.1 milestone Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant