Skip to content

std::optional serialization conflicts with boost::shared_ptr in Boost 1.84 and 1.85 #319

Open
@sowle

Description

@sowle

I'm experiencing a very strange compilation issue when using boost::shared_ptr with the (de)serialization of std::optional.

std::optional serialization support has been added recently in version 1.84.0: https://www.boost.org/doc/libs/1_84_0/boost/serialization/optional.hpp (see also commit 61a2b1207619cedb1f4d3ac1c7193f10ef9334f7)

After a lot of effort I have reduced the problem to a minimal reproducible example, which highlights the issue in both 1.84 and 1.85 versions (earlier versions don't support std::optional serialization).

MRE:

#include <fstream>
#include <iostream>
#include <optional>
#include <boost/filesystem/fstream.hpp>
#include <boost/serialization/optional.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

struct pluto_t
{
  uint64_t mass = 0;
  std::optional<uint64_t> optional_mass;

  template <class archive_t>
  void serialize(archive_t &_a, const unsigned int ver)
  {
    _a & mass;
    _a & optional_mass;                          // <-- no error if this line is commented out
  }

};


int main()
{
  const char* filename = "tmpfile";
  // store to file
  {
    pluto_t pluto{};
    pluto.mass = 13025000000000000000ull;

    std::ofstream ofs(filename);
    boost::archive::text_oarchive oa(ofs);
    oa << pluto;
  }

  // load from file
  {
    pluto_t pluto{};

    std::ifstream ifs(filename);
    boost::archive::text_iarchive ia(ifs);
    ia >> pluto;                                 // <-- no error if this line is commented out

    std::cout << pluto.mass << std::endl;
  }

  std::cout << "done." << std::endl;
  return 0;
}


#include <boost/serialization/shared_ptr.hpp>    // <-- no error if this line is commented out

[[maybe_unused]] static int foo = 42;

Error (there's lots of text, here's only the error line):

/usr/include/c++/8/type_traits:1147:12: error: invalid use of incomplete type ‘class boost::serialization::U’
     struct __is_trivially_copy_constructible_impl<_Tp, true>
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compilation flags:
g++ f.cpp -std=c++17 -g -O3 -Wall -DNDEBUG -o f -I$BOOST_ROOT -L$BOOST_ROOT/stage/lib -static -lboost_system -lboost_serialization -s

Compiler version:

g++ --version
g++ (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
Copyright (C) 2018 Free Software Foundation, Inc.

This example doesn't compile.
If any of the corresponding lines in the code are commented out, there's no error whatsoever.

UPD. It looks like a gcc-specific issue. I have no compilation issues with MSVC Community 2022 (64-bit) Version 17.10.4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions