Closed llaszko closed 5 years ago
Thanks @llaszko for reporting this issue. You are absolutely right about the behavior. Boost.DI design(v1.0.0) is not deducing (or even using) the scope in case when instance scope is used (binding using to
method). The reason behind such a design was the idea that instances provided by users will have specified life time already, however, this approach has a lot of limitations and therefore I'm planning to allow scopes deduction and scope specification in those cases for v1.1.0 (to be released in May/June 2016).
The only 'hacky' solution I can think of for now (v1.0.0) would be to specify the scope yourself which is far from begin ideal tho :(
auto injector = di::make_injector(
di::bind<ITypeInterface>().to([] {
static auto object = std::make_shared<TypeImplementation>();
return object;
}));
which will print...
Hallo from 0x997c30!
Hallo from 0x997c30!
I will keep this issue open as long as the proper solution won't be implemented.
Actually, I have very similar issue: I need to impose extended (shared) scope for dynamic binding. @krzysztof-jusiak do you have any plans to add this?
Is this issue still active? I'm using the latest cpp14 branch but I suspect it's not been addressed yet. I'm loving this library, great job guys.
It is addressed at shared_factory
extension.
Default scope doesn't recognize scope of a
shared_ptr
as singleton if a dynamic binding is used.Consider following code:
when compiled and executed the output prints out:
I'd expect in such case the factory lambda to be invoked once and the result to be reused as a singleton. Additionally, it's impossible to enforce singleton scope for this registration. If binding is amended to:
clang 3.7 complains about