[Serialization] Pointer to const

Hello everybody, There a still many stuff I do not understand how to do with boost::serialization. One of them is the following : Let's say I have the following classes : struct B { int i; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & i;} }; struct A { B const *pc; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & pc; } }; When I try to serialize a A, I get the following error (MSVC8 beta 2): c:\boost\include\boost-1_33\boost\archive\detail\iserializer.hpp(474) : error C2440: 'reinterpret_cast' : cannot convert from 'const B **__w64 ' to 'void **' Conversion loses qualifiers c:\boost\include\boost-1_33\boost\archive\detail\iserializer.hpp(471) : while compiling class template member function 'void boost::archive::detail::load_pointer_type<Archive,Tptr>::invoke(Archive &,Tptr &)' with [ Archive=boost::archive::text_iarchive, Tptr=const B * ] Of course, if I replace B const *pc by B *pc, then the code works correctly.What can I do to serialize pointers to const data ? Regards, -- Loïc

Of course, if I replace B const *pc by B *pc, then the code works correctly.What can I do to serialize pointers to const data ?
Regards,
Or you could replace this above with: ar & const_cast<B *>(pc); (or maybe const_cast<B * &>(pc) But that would override the normal meaning of "const" as applied to a member variable. You really have to ask yourself what you want serialize to include. Robert Ramey

Robert Ramey a écrit :
Of course, if I replace B const *pc by B *pc, then the code works correctly.What can I do to serialize pointers to const data ?
Regards,
Or you could replace this above with: ar & const_cast<B *>(pc); (or maybe const_cast<B * &>(pc)
I really do not understand why I would have to do that.
But that would override the normal meaning of "const" as applied to a member variable.
In this case, the member variable is not const. It's not like in http://www.boost.org/libs/serialization/doc/serialization.html#const . It's just a non const pointer that refers to a non const class defined elsewhere, but that promise not to modify that class himself.
You really have to ask yourself what you want serialize to include.
Could you please explain this point ? I do not understand what you mean. Best regards, -- Loïc

Loïc Joly wrote:
Robert Ramey a écrit :
Of course, if I replace B const *pc by B *pc, then the code works correctly.What can I do to serialize pointers to const data ?
Regards,
Or you could replace this above with: ar & const_cast<B *>(pc); (or maybe const_cast<B * &>(pc)
I really do not understand why I would have to do that.
But that would override the normal meaning of "const" as applied to a member variable.
In this case, the member variable is not const. It's not like in http://www.boost.org/libs/serialization/doc/serialization.html#const . It's just a non const pointer that refers to a non const class defined elsewhere, but that promise not to modify that class himself.
correct. Its a non-const pointer to a const object. But if the object is const then it is prohibited from being modified. Serialization has no special privleges in this regard. So loading it traps at compile time - just as would anyother attempt to modify the object that B is pointing to.
You really have to ask yourself what you want serialize to include.
Could you please explain this point ? I do not understand what you mean.
If you want the object pointed to by pc to be modifiable by serializaltion then it shouldn't be const. Robert Ramey

Robert Ramey a écrit :
Loïc Joly wrote:
Robert Ramey a écrit :
Of course, if I replace B const *pc by B *pc, then the code works correctly.What can I do to serialize pointers to const data ?
Regards,
Or you could replace this above with: ar & const_cast<B *>(pc); (or maybe const_cast<B * &>(pc)
But that would override the normal meaning of "const" as applied to a member variable.
In this case, the member variable is not const. It's not like in http://www.boost.org/libs/serialization/doc/serialization.html#const . It's just a non const pointer that refers to a non const class defined elsewhere, but that promise not to modify that class himself.
correct. Its a non-const pointer to a const object. But if the object is const then it is prohibited from being modified. Serialization has no special privleges in this regard.
My only issue is that the other objetc is not const, and in fact, it has already been saved before. It is just const seen from A point of view. But I understand that is would be quite difficult to differenciate such a case from a case where the only access to the B object is through A. I think I will use this const_cast. This is I think the first time I find any use for a const_cast. Thank you for your help, -- Loïc
participants (2)
-
Loïc Joly
-
Robert Ramey