google / fruit

Fruit, a dependency injection framework for C++

Home Page:https://github.com/google/fruit/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generic binding?

vrecluse opened this issue · comments

Is it possible to auto bind open generic types?
eg:

template <typename T>
struct Signal {...};

...
fruit::createComponent().bind<Signal<T>>();
...


auto sig_int = injector.get<Singal<int>*>():
auto sig_float = injector.get<Singal<float>*>():
... and others without  manual bind for every template type

TL;DR no, each injector can only have a finite number of types bound.

You can define a templated get*Component function and then install multiple instantiations of that for various types. But the set of bound types can't grow after the injector is created.

What are you trying to do?
To avoid the XY problem.

One option might be to bind a single type and then have a method there to construct a Signal. In that case you'd need to handle the lifetime of the Signal objects though (e.g. using unique_ptr or shared_ptr).

Ok, thanks.