Skip to content

[C++] std::unordered_map cannot be used in struct. #3727

@ruoruoniao

Description

@ruoruoniao

Search before asking

  • I had searched in the issues and found no similar issues.

Version

Compiler MSVC/C++ 17
Fory commit: f156374

Component(s)

C++

Minimal reproduce step

struct MapsInStruct {
  std::map<int, std::string> map{};
  std::unordered_map<std::string, std::string> unordered_map{};

  auto operator==(const MapsInStruct &rhs) const -> bool {
      return map == rhs.map && unordered_map == rhs.unordered_map;
  }

  FORY_STRUCT(MapsInStruct, map, unordered_map);
};

TEST(MapSerializerTest, UnorderedMapInStruct) {
  MapsInStruct original{};
  original.map[1] = "1";
  original.map[2] = "2";
  original.unordered_map["1"] = "2";
  original.unordered_map["2"] = "2";

  // Create Fory instance with default config
  auto fory = Fory::builder().xlang(true).compatible(true).build();

  fory.register_struct<MapsInStruct>(0);

  // Serialize
  auto serialize_result = fory.serialize(original);
  ASSERT_TRUE(serialize_result.ok())
      << "Serialization failed: " << serialize_result.error().message();
  auto bytes = serialize_result.value();

  // Deserialize
  auto deserialize_result = fory.deserialize<MapsInStruct>(bytes.data(), bytes.size());
  ASSERT_TRUE(deserialize_result.ok())
      << "Deserialization failed: " << deserialize_result.error().message();
  auto deserialized = deserialize_result.value();

  // Compare
  EXPECT_EQ(original, deserialized);
}

Compile it.

What did you expect to see?

Compile as normal.

What did you see instead?

This code fails to compile. The error message is:
error C2660: "fory::serialization::Serializer<fory::serialization::detail::CompileTimeFieldHelpers::field_needs_type_info_in_compatible::FieldType,void>::write": function does not take 5 arguments

Anything Else?

No response

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions