boost-ext / di

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

Impossible to bind a pointer to a value ? #317

Closed Toinouze closed 6 years ago

Toinouze commented 6 years ago

Expected Behavior

const auto injector = boost::di::make_injector(
            boost::di::bind<T*>.to(myTParam)
    );

should work

Actual Behavior

Gives error type_<T *>::has_disallowed_qualifiers>

Specifications

Shelim commented 6 years ago

Try:

const auto injector = boost::di::make_injector(
       boost::di::bind<>.to(myTParam)
);
krzysztof-jusiak commented 6 years ago

Right, bind can't have any qualifiers. It's by design to avoid binding all possible combinations (T, const T, volatile T*, T&, const T&, etc.). Instead, DI is taking care of the possible conversions (in your case from an instance).

Pointers, in general, are tricky as they can be owned or not. To avoid confusion DI takes non-owning references instead.

I think that https://github.com/boost-experimental/di/blob/cpp14/example/bind_non_owning_ptr.cpp is an example which is solving a similar problem.