boost-ext/di

const reference to shared_ptr injected via placeholder causing reference to temporary

mstaz opened this issue · 1 comments

mstaz commented

Expected Behavior

When a parameter is passed via const std::shared_ptr<T>& it gets resolved by di just as without const-ref or any other type.

Actual Behavior

Usually this works just fine. However when it gets injected via placeholders::_ directly or indirectly, it causes a reference to temporary warning. When parameter is used, e.g. calling a function from interface, a segfault happens.

di.hpp:2463:41: warning: returning reference to temporary [-Wreturn-local-addr]

Steps to Reproduce the Problem

Here is a code sample:

namespace di = boost::di;

struct interface {
  virtual ~interface() noexcept = default;
};

struct implementation : interface {
};

struct user {
  explicit user(const std::shared_ptr<interface>& instance) {
  }
};


int main() {
  const auto injector = di::make_injector(
     di::bind<interface>().to<implementation>(),
     di::bind<user>(di::placeholders::_)
    );

  injector.create<user>();
}

Link to CompilerExplorer
When the binding of user to placeholder::_ is removed it works fine.
When const-ref is removed from user's constructor it works fine as well.
Adding intermediate types also doesn't make a change.
Same behavior with clang and gcc.
I already checked in the code, but couldn't find the place where the issue happens.

mstaz commented

After debugging a little bit I found the necessary change to solve it. PR created and already merged by @krzysztof-jusiak.