Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#10488 closed Bugs (fixed)

Compilation of serializing shared_ptr<const Object> fails

Reported by: visa.jokelainen@… Owned by: Robert Ramey
Milestone: To Be Determined Component: serialization
Version: Boost 1.56.0 Severity: Regression
Keywords: shared_ptr serialization const compilation Cc: boost@…, mika.fischer@…

Description

Compilation of code that includes serialization of shared_ptr<const Object> fails with Boost 1.56.0 with error:

/usr/include/boost/smart_ptr/shared_ptr.hpp:323:25: note:   no known conversion for argument 1 from ‘const boost::shared_ptr<const TestClass>’ to ‘const boost::shared_ptr<void>&’

This works with Boost 1.55.0 and earlier versions. Tested on Arch Linux with clang 3.4.2 and g++ 4.9.1.

Here's a little test to replicate the problem:

#include <fstream>

#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>

#include <boost/make_shared.hpp>


class TestClass
{
public:

    TestClass(int i) : _i(i) {}
    
private:
    
    friend class boost::serialization::access;

    TestClass() {}

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    { 
        ar & _i;
    }

    int _i;
};

typedef boost::shared_ptr<TestClass> ClassPtr;
typedef boost::shared_ptr<const TestClass> ConstClassPtr;

int main(int argc, char* argv[])
{    
    ClassPtr ptr = boost::make_shared<TestClass>(13);
    ConstClassPtr cPtr = boost::make_shared<const TestClass>(42);

    {
        std::ifstream ifs("test");
        boost::archive::text_iarchive ia(ifs);

        ia >> ptr; // This works
        
        ia >> cPtr; // This doesn't
    }
    
    return 0;
}

Change History (16)

comment:1 by visa.jokelainen@…, 8 years ago

The problem sounds a lot like regression of this previous ticket: https://svn.boost.org/trac/boost/ticket/3123

comment:2 by Robert Ramey, 8 years ago

Actually this change looks like an improvement to me.

a const TestClass is something that shouldn't be modifiable

ia >> cPtr would do just that.

I don't remember any specific change which would affect this, but the current behavior seems to be the correct one to me.

comment:3 by Robert Ramey, 8 years ago

Resolution: duplicate
Status: newclosed

comment:4 by reder <rouge.richard@…>, 8 years ago

This is a regression. Not being able to serialize a shared_ptr<const Object> is indeed very painful. Ticket 3123 shows it is not absurd: imagine I have a

typedef map<string,shared<book>> library;

Since I do not want people to write on the books, I'd prefer a

typedef map<string,shared<const book>> library;

and be able to write/load the contents of my library.

Anyhow, it seems to be just a matter of changing line 59 of serialization/shared_ptr_helper.cpp from

  SPT<void> // address shared ptr to single instance

to

  SPT<const void> // address shared ptr to single instance

to regain previous behaviour (if one can check extensively).

comment:5 by Robert Ramey, 8 years ago

Resolution: duplicate
Status: closedreopened

comment:6 by tonvandenheuvel@…, 8 years ago

I can confirm the fix proposed by reder works for me, although I have not checked this extensively.

comment:7 by chris@…, 8 years ago

This regression currently prevents building Cufflinks; the author is disinclined to work around the problem. I can't link the issue directly due to the spam trap, but google "cufflinks boost serialization".

comment:8 by Robert Ramey, 8 years ago

Resolution: fixed
Status: reopenedclosed

note: already checked into master. Correction will appear in next boost release

comment:9 by boost@…, 8 years ago

Cc: boost@… added

Which commit fixed this problem? I would like to try to backport it to boost 1.57.0 in MacPorts.

comment:10 by boost@…, 8 years ago

I tested with boost 1.58.0 beta 1, released March 16, 2015, and the problem remains.

comment:11 by Mika Fischer <mika.fischer@…>, 8 years ago

Cc: mika.fischer@… added

comment:12 by boost@…, 8 years ago

I tested with boost 1.58.0, released April 17, 2015, and the problem remains.

If the fix was supposed to be to change SPT<void> to SPT<const void> in https://github.com/boostorg/serialization/blob/master/include/boost/serialization/shared_ptr_helper.hpp, that has not occurred.

comment:13 by anonymous, 7 years ago

OK - I double checked - I fixed this again - I'm not sure what happened the first time

comment:14 by boost@…, 7 years ago

Could you please post a link to the commit where you fixed it, so that I can backport it to boost 1.58.0 in MacPorts? Thanks.

comment:16 by Robert Ramey, 7 years ago

I believe so

Note: See TracTickets for help on using tickets.