Skip to content

Support std::map<..., std::less<>> (transparent comparator) in JSON read/write #561

@RampantDespair

Description

@RampantDespair

reflect-cpp currently supports std::map<K, V> with the default comparator, but fails when the map uses a transparent comparator like std::less<>.

This is a common modern C++ pattern because std::less<> enables heterogeneous lookup (e.g. map.find(std::string_view) without allocating a std::string). It would be great if reflect-cpp treated std::map<K, V, Compare> the same as long as it’s still a std::map and the key/value types are supported.

Minimal Repro

Works

#include <map>
#include <string>
#include <string_view>

#include <rfl.hpp>
#include <rfl/json.hpp>
#include <rfl/patterns.hpp>

struct Request {
    std::map<std::string, std::string> headers;

    inline static auto fromJson(std::string_view v) {
        return rfl::json::read<Request>(v);
    }
};

Fails

#include <functional>
#include <map>
#include <string>
#include <string_view>

#include <rfl.hpp>
#include <rfl/json.hpp>
#include <rfl/patterns.hpp>

struct Request {
    std::map<std::string, std::string, std::less<>> headers;

    inline static auto fromJson(std::string_view v) {
        return rfl::json::read<Request>(v);
    }
};

Actual Behavior

Compilation fails with a static assertion / unsupported type error.

Example (MSVC):

  • C2338 static assertion failed: 'Unsupported type.'

Expected Behavior

std::map<std::string, std::string, std::less<>> should serialize/deserialize the same way as std::map<std::string, std::string>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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