Opened 13 years ago
Closed 13 years ago
#3123 closed Bugs (fixed)
Support for serializing shared_ptr to const types
Reported by: | Sohail Somani | Owned by: | Robert Ramey |
---|---|---|---|
Milestone: | Boost 1.40.0 | Component: | serialization |
Version: | Boost 1.39.0 | Severity: | Regression |
Keywords: | Cc: | boost-trac@… |
Description
Attached is a patch. This used to work in 1.34.1 but no longer works. Not sure if that was intentional or not (why?)
Attachments (1)
Change History (11)
by , 13 years ago
Attachment: | serialization.diff added |
---|
follow-ups: 3 4 comment:1 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Looking at the diff, I would rather call this "serialization of shared_ptr to a const type".
The subject if serialization of "const" members is addressed in general in the manual in the section Reference/SerializableConcept/ClassTypes/ClassMembers/constMembers.
From the documentation
"Saving const members to an archive requires no special considerations. Loading const members can be addressed by using a const_cast:
ar & const_cast<T &>(t);
Note that this violates the spirit and intention of the const keyword. const members are intialized when a class instance is constructed and not changed thereafter. However, this may be most appropriate in many cases. Ultimately, it comes down to the question about what const means in the context of serialization. "
Basically, "const" means the member isn't meant to be changeable. It would be in appropriate for the serialization library (or any other library) to violate the explicitly stated intention - especially without calling attention to that fact. It the developer want's to override the "const" the special case of serialization, he has the option of using const_cast to implement and document this desire.
If this used to work, it was an oversight which has since been corrected.
Robert Ramey
comment:2 by , 13 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
I have to disagree with you here. We are not touching a const object at all. The shared_ptr itself is non-const.
T *p; p is non-const T const *p; p is still non-const
shared_ptr<T> p; p is non-const shared_ptr<const T> p; p is still non-const
So what is the problem?
comment:3 by , 13 years ago
Replying to ramey:
Fixing formatting.
I have to disagree with you here. We are not touching a const object at all. The shared_ptr itself is non-const.
T *p; // p is non-const T const *p; // p is still non-const
shared_ptr<T> p; // p is non-const shared_ptr<const T> p; // p is still non-const
So what is the problem? I don't need a const-cast to mutate p in either case. This to me is a deficiency in the library not the user!
comment:4 by , 13 years ago
Replying to ramey:
Looking at the diff, I would rather call this "serialization of shared_ptr to a const type".
Yes, you are completely right about that. Bad wording on my part.
comment:5 by , 13 years ago
Cc: | added |
---|
comment:6 by , 13 years ago
Note that I've checked in changes which touch upon this issue. This should be tested again with the trunk or release branch versions.
Robert Ramey
comment:7 by , 13 years ago
Still doesn't seem to work. Can you point me to the revision where you checked in? Maybe I am checking out the wrong branch.
Thanks.
comment:8 by , 13 years ago
I'm taking another look at this.
The first thing that occurs to me is that this has nothing to do with shared_ptr serialization. The same situation occurs with anything like:
const B * b; ar & b;
I'm still looking at it.
Robert Ramey
comment:10 by , 13 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
I did manage to "fix" this. I had to include a blunt hack in iserializer.hpp to accomplish this. C++ is very, very reluctant to permit casting from const T a type T.
Patch to support loading and saving const shared_ptr