Skip to content

Possible Operator Usage/Deduction Error when Using REGISTER_STRUCT macros #456

@fwilmsh

Description

@fwilmsh

Describe the bug
It seems I cannot use a custom serialisation operator for std::optional, when the optional is used as a direct struct member.
It works when I wrap a custom type/struct into the std::optional though.

To Reproduce
See code example below.

Expected behavior
Both versions from example compile?

Real (buggy) behavior
It seems during usage of the macro lib tries to use one of its own operators before seeing the custom definition, but I'm guessing here.

Additional context
sdbus-cpp from master, (c6705fa), version contains bug fixes for some enum handling I required earlier.

// Type Helpers for std::optional
#include "sdbus-c++/sdbus-c++.h"
#include <optional>

template<typename T>
struct sdbus::signature_of<std::optional<T>> : public sdbus::signature_of<sdbus::Variant>{};

template<typename T>
inline sdbus::Message& operator<<(sdbus::Message& os, const std::optional<T> maybeValue){
  static_assert(!std::is_same_v<T, bool>);
  if(maybeValue){
    os << sdbus::Variant(maybeValue.value());
  }
  else {
    os << sdbus::Variant(false);
  }
  return os;
}

template<typename T>
inline sdbus::Message& operator>>(sdbus::Message& is, std::optional<T>& maybeValue){
  static_assert(!std::is_same_v<T, bool>);
  sdbus::Variant tmp;
  is >> tmp;
  if(tmp.containsValueOfType<T>()){
    maybeValue = tmp.get<T>();
  } else {
    maybeValue = std::nullopt;
  }
  return is;
}

// This is working fine
struct Foo {
        int member;
};

struct Bar {
    std::optional<Foo> foo;
};

SDBUSCPP_REGISTER_STRUCT(Foo, member);
SDBUSCPP_REGISTER_STRUCT(Bar, foo);

// When using a type for which operators are already provided by the lib itself,
// it seems the template providers up top are not used/found by the compiler.
struct Foobar {
    std::optional<int> foobar;
};

SDBUSCPP_REGISTER_STRUCT(Foobar, foobar);

I'm basically looking for some guidance on this issue. Not really sure if its a bug with the lib or if I'm using it just wrong. Let me know if I can provide more info on this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions