-
-
Notifications
You must be signed in to change notification settings - Fork 186
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
Add constructor for box<T> from box<Y> for compatible Y #157
Comments
Hmmm, I find the suggestion interesting, but I'm not sure supporting sharing in this case (instead of slicing) justifies the added complexity. In any case due to immutability the two cases (slicing or sharing) should be semantically equivalent. If we go down this road, note that you are most probably using inheritance for composition. One could also consider this other use-case, which is actually supported by shared ptr as well: struct A { ... };
struct B
{
A member;
};
void example()
{
immer::box<B> b;
immer::box<A> a{b.member, b};
} |
I see what you mean. I'm going to give an extremely contrived example of my usecase: struct Product
{
int id;
};
struct Hat : Product
{
Color color;
};
struct CustomerOrder
{
immer::box<Product> product;
}; We're writing a Without downcasting, sharing and slicing would be semantically equivalent, I agree. Here I was trying to avoid putting more templates than necessary, and that involves "knowledge outside the type system" that all |
Hi! Sorry for the late reply, I was on vacation :) I see, that is a bit of an unorthodox design, I am unsure yet what the full consequences of that would be (How do you discriminate the types for downcasting? Is there a place where you can query any product? etc.). Normally you would just store the If this is for a commercial project, send me an email, we can maybe arrange a contract to develop this feature in the library, but maybe I can help you review the design to avoid these pitfalls. |
Similar to how https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr has a constructor (9) that participates in overload resolution if
Y*
is compatible withT*
, that can work forbox<T>
.E.g. something like:
I don't think there should be concerns with object slicing since the values held are immutable and are not copied/moved by immer. I think implementation-wise this would mean a virtual function or some function pointer in the
MemoryPolicy::refcount
class that does deletion. What do you think?The text was updated successfully, but these errors were encountered: