eyalz800 / zpp_bits

A lightweight C++20 serialization and RPC library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot serialize optional unique pointer

rael007 opened this issue · comments

I read the documentation which indicated that optional<unique_ptr> was supported by zpp bits. Not sure if I'm doing something very stupid, but using gcc-13 on Mac, I cannot get the following code to compile.

#include "zpp_bits.h"
#include <memory>
#include <string>
#include <vector>

namespace zzz
{
template < class T, class B >
std::size_t serialize( const T& t, B& data )
{
    zpp::bits::out out{ data };
    if ( auto result = out( t ); failure( result ) ) { return 0; }
    return out.position();
}

template < class T, class B >
std::size_t deserialize( T& t, const B& data )
{
    zpp::bits::in in{ data };
    if ( auto result = in( t ); failure( result ) ) { return 0; }
    return in.position();
}

}

struct foo
{
    std::string name;
    int         count;
};

struct bar
{
    std::optional< std::unique_ptr< foo > > foos;
};

int main()
{
    {
        std::vector< char >                     data;
        std::optional< std::unique_ptr< foo > > foos;
        // compiles ok:
        zzz::serialize( foos, data );
    }

    {
        std::vector< char > data;
        // does not compile:
        bar b;
        zzz::serialize( b, data );
    }
}
commented

Hi,

Thanks for reporting, do you mind reproducing this in godbolt and confirming this is the same error you receive on your Mac, then attach the godbolt link?

Thanks

Ok, or you can just mark me as an idiot. Of course, after posting this I figured out how to get around the issue:

struct bar
{
    std::optional< std::unique_ptr< foo > > foos;
};

auto serialize( const bar& bar ) -> zpp::bits::members< 1 >;

Sorry for the false alarm, Eyal.