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)

serialization.diff (6.0 KB ) - added by Sohail Somani 13 years ago.
Patch to support loading and saving const shared_ptr

Download all attachments as: .zip

Change History (11)

by Sohail Somani, 13 years ago

Attachment: serialization.diff added

Patch to support loading and saving const shared_ptr

comment:1 by Robert Ramey, 13 years ago

Resolution: invalid
Status: newclosed

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 Sohail Somani, 13 years ago

Resolution: invalid
Status: closedreopened

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?

in reply to:  1 comment:3 by Sohail Somani, 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!

in reply to:  1 comment:4 by Sohail Somani, 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 Sohail Somani, 13 years ago

Cc: boost-trac@… added

comment:6 by Robert Ramey, 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 Sohail Somani, 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 Robert Ramey, 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:9 by Sohail Somani, 13 years ago

Thanks, I appreciate it.

comment:10 by Robert Ramey, 13 years ago

Resolution: fixed
Status: reopenedclosed

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.

Note: See TracTickets for help on using tickets.