boost-ext / di

C++14 Dependency Injection Library
https://boost-ext.github.io/di
1.13k stars 136 forks source link

Dynamic bindings don't work with explicit singleton scope #548

Open scztt opened 1 year ago

scztt commented 1 year ago

Expected Behavior

#include <boost/di.hpp>

struct trivial{};
struct trivial_impl : public trivial {};

int main() {
  const auto injector = boost::di::make_injector(
        boost::di::bind<trivial>.to([](auto const& inj) -> std::shared_ptr<trivial> {
            return inj.template create<std::shared_ptr<trivial_impl>>();
        })
        // does not compile
        // .in(boost::di::singleton)
  );

  auto t = injector.create<std::shared_ptr<trivial>>();
}

(https://godbolt.org/z/rzvja1PqM)

My expectation is that uncommenting the .in line is valid and should compile. Moreover, my impression is that singleton scope is the default, so this code should be semantically identical with or without the commented line.

Actual Behavior

Compile error related to "creatable constraint not satisfied". None of the obvious alternative return values from the lambda work here, nor do obvious variations on the create call (e.g. create<trivial&>). The above failure also applies to other scopes like extension::session, but this may be because they wrap singleton.