cereal icon indicating copy to clipboard operation
cereal copied to clipboard

load_minimal cannot find input serialization function for class with two template parameters

Open vsamokhvalov opened this issue 2 years ago • 1 comments

The following is a minimal example which does not compile for me under C++ 17:

#include <cereal/archives/json.hpp>

template<class T, class P>
class NamedType
{
public:
    NamedType() = default;
    NamedType(T value) : _value(value) {}
    const T& get() const { return _value; }

private:
    T _value;
};

namespace cereal
{
template <typename Archive, typename T, typename P>
T save_minimal(Archive const&, const NamedType<T, P>& t)
{
    return t.get();
}

/**
     * Cereal deserialization for binary and text (JSON) only.
 */
template <typename Archive, typename T, typename P>
void load_minimal(Archive const&, NamedType<T, P>& t, T const& value)
{
    t = NamedType<T, P>(value);
}
}

int main()
{
    using NamedSize = NamedType<size_t, struct Size>;

    NamedSize c(10);
    std::ostringstream os;
    {
        cereal::JSONOutputArchive arout(os);
        arout(CEREAL_NVP(c));
    }

    std::string tmp = os.str();
    std::cout << tmp << "\n";

    std::stringstream ss2(tmp);
    {
        cereal::JSONInputArchive arin(ss2);
        NamedSize c2;
        arin(c2);
    }

    return 0;
}

The compiler outputs something to the effect of:

tools/cereal-1.3.2/include/cereal/cereal.hpp: In instantiation of ‘ArchiveType& cereal::InputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = NamedType<long unsigned int, main()::Size>; typename cereal::traits::detail::EnableIfHelper<(cereal::traits::has_invalid_input_versioning<T, ArchiveType>::value || ((! cereal::traits::is_input_serializable<T, ArchiveType>::value) && ((!(Flags & cereal::AllowEmptyClassElision)) || ((Flags & cereal::AllowEmptyClassElision) && (! std::is_empty<T>::value)))))>::type <anonymous> = (cereal::traits::detail::sfinae)0; ArchiveType = cereal::JSONInputArchive; unsigned int Flags = 0]’:
/ufo/tools/cereal-1.3.2/include/cereal/cereal.hpp:853:26:   required from ‘void cereal::InputArchive<ArchiveType, Flags>::process(T&&) [with T = NamedType<long unsigned int, main()::Size>&; ArchiveType = cereal::JSONInputArchive; unsigned int Flags = 0]’
/ufo/tools/cereal-1.3.2/include/cereal/cereal.hpp:730:16:   required from ‘ArchiveType& cereal::InputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {NamedType<long unsigned int, main()::Size>&}; ArchiveType = cereal::JSONInputArchive; unsigned int Flags = 0]’


/tools/cereal-1.3.2/include/cereal/cereal.hpp:985:86: error: static assertion failed: cereal could not find any input serialization functions for the provided type and archive combination. 

 Types must either have a serialize function, load/save pair, or load_minimal/save_minimal pair (you may not mix these). 
 Serialize functions generally have the following signature: 


 template<class Archive> 
   void serialize(Archive & ar) 
   { 
     ar( member1, member2, member3 ); 
   } 

 
  985 |         static_assert(traits::detail::count_input_serializers<T, ArchiveType>::value != 0,

This is not an issue when serializing with save and load functions.

vsamokhvalov avatar Jun 08 '23 09:06 vsamokhvalov

您的邮件我已收到,谢谢合作!

redchairman avatar Jun 08 '23 09:06 redchairman