You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my case i want to share a logger instance through my classes therefore I pass some const std::shared_ptr<T>& into my classes.
Actually i cant change my constructor arguments, because i have to check if boost::di can be used non-invasive.
Is there a way to enforce the right deduction of const std::shared_ptr<T>& ?
Or how to handle this case?
auto log = std::make_shared<logger>();
auto injector = di::make_injector(
di::bind<logger>().to(log),
di::bind<MyType>, // needs a const std::shared_ptr<logger>&
di::bind<ANestedType> // needs a const std::shared_ptr<logger>&
);
Expected Behavior
Each instance of my class is using the same logger.
Logoutput # 0 msg: MyType created
Logoutput # 1 msg: ANestedType created
Logoutput # 2 msg: log from ANestedType
Logoutput # 3 msg: log from MyType
Actual Behavior
The Logger which shall be passed by const std::shared_ptr<T>& is somehow dangling.
In my example, it is obv not the same logger instance. Even the address sanitizer is complaining.
Logoutput # 0 msg: MyType created
Logoutput # 1843005592 msg: ANestedType created
Logoutput # 1843005593 msg: log from ANestedType
Logoutput # 1 msg: log from MyType
Note sure why scoped instance would not participate in the scope deduction for nested types but that seems the case.
It might be easier to create a logger with di as well. That seems to work but not sure whether it fits your use case? https://godbolt.org/z/ejrT6hqK3
Thx for the impressing fast response @krzysztof-jusiak :)
I stripped down the Problem to this simple example but my actual use case looks like this:
LoggerFactory will be created once with some config files
LoggerFactory is the only one who can create std::shared_ptr<logger>
MyClass( const std::shared_ptr& logger)
OtherClass( const std::shared_ptr& logger)
So within the injector i need one LoggerFactory and i thought i have to bind the const std::shared_ptr<logger>& to the create() Function of the LoggerFactory.
Context
In my case i want to share a logger instance through my classes therefore I pass some
const std::shared_ptr<T>&
into my classes.Actually i cant change my constructor arguments, because i have to check if boost::di can be used non-invasive.
Is there a way to enforce the right deduction of
const std::shared_ptr<T>&
?Or how to handle this case?
Expected Behavior
Each instance of my class is using the same logger.
Actual Behavior
The Logger which shall be passed by
const std::shared_ptr<T>&
is somehow dangling.In my example, it is obv not the same logger instance. Even the address sanitizer is complaining.
Steps to Reproduce the Problem
const std::shared_ptr<logger>& log
Specifications
The text was updated successfully, but these errors were encountered: