Loki-Astari / ThorsMongo

C++ MongoDB API and BSON/JSON Serialization library
GNU General Public License v3.0
316 stars 72 forks source link

error compiling with std::shared_ptr<const Type> #83

Closed agamal94 closed 9 months ago

agamal94 commented 9 months ago

Describe the bug Hi, I'm experiencing an error trying to use shared pointers for const types. on the other hand if const was removed it works perfectly fine. std::shared_ptr member;. I wonder if something like this is not supported, or could be an easy fix.

Example snippet

struct Example { int x{0}; }; struct Adeek { std::shared_ptr member; };

ThorsAnvil_MakeTrait(Example, x); ThorsAnvil_MakeTrait(Adeek, member);

Adeek obj; outputJSONfile << jsonExporter(obj);

error:

build] /code/include/json/ThorSerialize/Serialize.tpp: In instantiation of 'class ThorsAnvil::Serialize::SerializerForBlock<ThorsAnvil::Serialize::TraitType::Invalid, const Example>': [build] /code/include/json/ThorSerialize/Serialize.tpp:798:82: required from 'void ThorsAnvil::Serialize::tryPrintPolyMorphicObject(ThorsAnvil::Serialize::Serializer&, ThorsAnvil::Serialize::PrinterInterface&, const T&, long int) [with T = std::shared_ptr]' [build] /code/include/json/ThorSerialize/Serialize.tpp:835:42: required from 'void ThorsAnvil::Serialize::SerializerForBlock<ThorsAnvil::Serialize::TraitType::Pointer, T>::printMembers() [with T = std::shared_ptr]' [build] /code/include/json/ThorSerialize/Serialize.tpp:953:9: required from 'void ThorsAnvil::Serialize::SerializeMemberValue<T, M, Type>::init(ThorsAnvil::Serialize::Serializer&, ThorsAnvil::Serialize::PrinterInterface&, const char, const T&, const M&) [with T = Adeek; M = std::shared_ptr; ThorsAnvil::Serialize::TraitType Type = ThorsAnvil::Serialize::TraitType::Pointer]' [build] /code/include/json/ThorSerialize/Serialize.tpp:937:5: required from 'ThorsAnvil::Serialize::SerializeMemberValue<T, M, Type>::SerializeMemberValue(ThorsAnvil::Serialize::Serializer&, ThorsAnvil::Serialize::PrinterInterface&, const T&, const std::pair<const char, M T::>&) [with T = Adeek; M = std::shared_ptr; ThorsAnvil::Serialize::TraitType Type = ThorsAnvil::Serialize::TraitType::Pointer]' [build] /code/include/json/ThorSerialize/Serialize.tpp:962:23: [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ] [build] /code/include/json/ThorSerialize/Serialize.tpp:988:5: required from 'void ThorsAnvil::Serialize::Serializer::printMembers(const T&, const std::tuple<_Elements ...>&) [with T = Adeek; Members = {std::pair<const char, std::shared_ptr Adeek::*>}]' [build] /code/include/json/ThorSerialize/Serialize.tpp:1027:5: required from 'void ThorsAnvil::Serialize::Serializer::printObjectMembers(const T&) [with T = Adeek]' [build] /code/include/json/ThorSerialize/Serialize.tpp:700:13: required from 'void ThorsAnvil::Serialize::SerializerForBlock<traitType, T>::printMembers() [with ThorsAnvil::Serialize::TraitType traitType = ThorsAnvil::Serialize::TraitType::Map; T = Adeek]' [build] /code/include/json/ThorSerialize/Serialize.tpp:1001:5: required from 'void ThorsAnvil::Serialize::Serializer::print(const T&) [with T = Adeek]' [build] /code/include/json/ThorSerialize/Exporter.h:35:17: required from 'std::ostream& ThorsAnvil::Serialize::operator<<(std::ostream&, const ThorsAnvil::Serialize::Exporter<ThorsAnvil::Serialize::Json, Adeek>&)' [build] /code/src/example.cxx:511:40: required from here [build] /code/include/json/ThorSerialize/Serialize.tpp:674:19: error: static assertion failed: Invalid Serialize TraitType. This usually means you have not define ThorsAnvil::Serialize::Traits [build] 674 | traitType != TraitType::Invalid,

Loki-Astari commented 9 months ago

I added this test: https://github.com/Loki-Astari/ThorsSerializer/blob/master/src/Serialize/test/Issue83Test.cpp

Seems to compile for me. Let me know if this is not what you are doing.

Loki-Astari commented 9 months ago

OK. I figured it out (the issue).

If I change the shared_ptr

std::shared_ptr<const Example> member;

Then I see some compilation errors.

Loki-Astari commented 9 months ago

OK pushed a fix to master. The changes are in src/Serialize/SerUtil.h lines 912 and 925. https://github.com/Loki-Astari/ThorsSerializer/blob/b339cd952eff09f62fda2e4244bf92a0ca0bd7cc/src/Serialize/SerUtil.h#L925

Added: std::remove_cv_t<T>

I am in the middle of another big change. So it may be a week or so before I update brew.

Loki-Astari commented 9 months ago

Added src/Serialize/test/Issue83Test.cpp to test for this condition.

agamal94 commented 9 months ago

yea sorry, forgot to check the formatting after posting. works fine now. thanks for your time!