Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DI Runtime Injector fails to compile for bind to a value #535

Open
hanexthink opened this issue Jan 28, 2022 · 3 comments
Open

DI Runtime Injector fails to compile for bind to a value #535

hanexthink opened this issue Jan 28, 2022 · 3 comments

Comments

@hanexthink
Copy link

Expected Behavior

Should compile

Actual Behavior

/Users/hafshari/.conan/data/di/1.2.0/jenkins-clt/stable/package/964905e09401b68ee5f6b695b187d4fa9eb760c2/include/boost/di/extension/providers/runtime_provider.hpp:125:14: error: call to member function 'make' is ambiguous
return make(binding);
^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:15:11: note: in instantiation of function template specialization 'boost::di::extension::detail::injector<boost::di::extension::assert_error_policy, boost::di::extension::shared_config>::install<boost::di::core::dependency<boost::di::scopes::instance, A>, 0>' requested here
injector.install(bind.to(A(1)));
^
/Users/hafshari/.conan/data/di/1.2.0/jenkins-clt/stable/package/964905e09401b68ee5f6b695b187d4fa9eb760c2/include/boost/di/extension/providers/runtime_provider.hpp:158:8: note: candidate function [with T = A, TBinding = boost::di::core::dependency<boost::di::scopes::instance, A>]
auto make(const TBinding &) -> decltype(make_impl(typename ctor_traits::type{})) {
^
/Users/hafshari/.conan/data/di/1.2.0/jenkins-clt/stable/package/964905e09401b68ee5f6b695b187d4fa9eb760c2/include/boost/di/extension/providers/runtime_provider.hpp:163:8: note: candidate function [with T = A, TBinding = boost::di::core::dependency<boost::di::scopes::instance, A>]
auto make(const TBinding &binding) -> decltype(new T{binding.object_}) {
^
1 error generated.
make: *** [main] Error 1

Steps to Reproduce the Problem

  1. You could verify the issue compiling the following source code

#include <boost/di.hpp>
#include <boost/di/extension/providers/runtime_provider.hpp>

using namespace boost::di;

struct A {
A(int) {};
virtual int now() {return 1;};
};

int main()
{
auto inj = make_injector();
extension::runtime_injector injector;
injector.install(bind.to(A(1)));
return 0;
}

Specifications

  • Version: v1.2.0
  • Platform: macOS Monterey 12.1
  • Subsystem:
@hanexthink hanexthink changed the title DI Runtime fails to compile for bind to a value DI Runtime Injector fails to compile for bind to a value Jan 28, 2022
@xoltar
Copy link

xoltar commented Jun 23, 2022

This code doesn't look valid to me. You have to tell bind what you're binding, with a type argument. This version builds and runs on latest MacOS 12.4.

#include <boost/di.hpp>
#include <boost/di/extension/providers/runtime_provider.hpp>
#include <iostream>

using namespace boost::di;

struct A {
  A(int) {};
  virtual int now() {return 1;};
};

int main(int argc, char** argv)
{
  extension::runtime_injector injector;
  injector.install(bind<A>.to(new A(1)));

  auto a1 = injector.create<A>();
  std::cout << "now: " << a1.now() << std::endl;
  return 0;
}

@hanexthink
Copy link
Author

hanexthink commented Jun 28, 2022

Hi @xoltar , thanks for your prompt reply the code that you mentioned does work. But I noticed that we can't still bind to an instance where its lifetime is controlled by the user in case of using a runtime dependency injector. Can we actually pass a defined instance to a runtime injector?

#include <boost/di.hpp>
#include <boost/di/extension/providers/runtime_provider.hpp>
#include

using namespace boost::di;

struct A {
A(int i) : m_i(i) {};
int now() const {return m_i;};
int m_i;
};

int main(int argc, char** argv)
{
extension::runtime_injector injector;
A b(1);
injector.install(bind.to(&b));

std::cout << "now: " << b.now() << std::endl;
const A &a1 = injector.create<A &>();
a1.now();
std::cout << "now: " << a1.now() << std::endl;
return 0;
}

@0renlyhuang
Copy link

Is there a way to bind a existed instance to a type using runtime_injector, like this ?
`
struct A {
A() {
}
};

void fun() {
auto a = std::make_shared();
di::extension::runtime_injector injector = {};
injector.install(di::bind.to(a));
}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants