boost-ext / di

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

Factory with of object with shared_ptr parameter not compiling on gcc 13.2.1 and gcc 12.2.1 #559

Open sva522 opened 4 months ago

sva522 commented 4 months ago

Dear,

This code is working fine :

#include <boost/di/extension/injections/factory.hpp>
#include <cassert>
#include <memory>

// g++ --version == 13.2.1
// g++ -O0 -g3 -std=gnu++20 -Wall -Wextra -Wpedantic -Werror -Wfatal-errors

class Interface
{
public:
    virtual ~Interface() noexcept = default;
};

class Implementation : public Interface
{
public:
    Implementation(int value) : value_(value) {}
    int value_ = 0;
};

class Example
{
public:
    Example(const boost::di::extension::ifactory<Interface, int>& f)
    {
        auto impl = f.create(87);
        assert(dynamic_cast<Implementation*>(impl.get()));
    }
};

namespace di = boost::di;

int main()
{
    auto injector = di::make_injector(di::bind<di::extension::ifactory<Interface, int>>().to(
        di::extension::factory<Implementation>()));
    auto example = injector.create<std::unique_ptr<Example>>();
    assert(example);
}

But this one does not compile :

#include <boost/di/extension/injections/factory.hpp>
#include <cassert>
#include <memory>

// g++ --version == 13.2.1
// g++ -O0 -g3 -std=gnu++20 -Wall -Wextra -Wpedantic -Werror -Wfatal-errors

class Interface
{
public:
    virtual ~Interface() noexcept = default;
};

class Implementation : public Interface
{
public:
    Implementation(std::shared_ptr<int> value) : value_(move(value)) {}
    std::shared_ptr<int> value_;
};

class Example
{
public:
    Example(const boost::di::extension::ifactory<Interface, std::shared_ptr<int>>& f)
    {
        auto ptr = std::make_shared<int>(87);
        auto impl = f.create(std::shared_ptr<int>(ptr));  // can i just pass `ptr` ?
        assert(dynamic_cast<Implementation*>(impl.get()));
    }
};

namespace di = boost::di;

int main()
{
    auto injector =
        di::make_injector(di::bind<di::extension::ifactory<Interface, std::shared_ptr<int>>>().to(
            di::extension::factory<Implementation>()));
    auto example = injector.create<std::unique_ptr<Example>>();
    assert(example);
}

Output is : In file included from workdir/lib/boost/di/extension/injections/factory.hpp:11, from workdir/tests/example2.cpp:1: workdir/lib/boost/di.hpp: In instantiation of 'struct boost::ext::di::v1_3_0::aux::concept_check<boost::ext::di::v1_3_0::concepts::type_<std::shared_ptr<int> >::has_disallowed_qualifiers>': workdir/lib/boost/di.hpp:1998:9: required from 'std::unique_ptr<I> boost::ext::di::v1_3_0::extension::factory_impl<TInjector, T, boost::ext::di::v1_3_0::extension::ifactory<I, TArgs ...> >::create(TArgs&& ...) const [with TInjector = boost::ext::di::v1_3_0::core::injector<boost::ext::di::v1_3_0::config, boost::ext::di::v1_3_0::core::pool<boost::ext::di::v1_3_0::aux::type_list<> >, boost::ext::di::v1_3_0::core::dependency<boost::ext::di::v1_3_0::scopes::instance, boost::ext::di::v1_3_0::extension::ifactory<Interface, std::shared_ptr<int> >, boost::ext::di::v1_3_0::extension::factory<Implementation>, boost::ext::di::v1_3_0::no_name, void, boost::ext::di::v1_3_0::core::none> >; T = Implementation; I = Interface; TArgs = {std::shared_ptr<int>}]' workdir/lib/boost/di/extension/injections/factory.hpp:30:22: required from here workdir/lib/boost/di.hpp:379:20: error: static assertion failed: constraint not satisfied 379 | static_assert(T::value, "constraint not satisfied"); | ^~~~~ compilation terminated due to -Wfatal-errors. make[2]: *** [CMakeFiles/tu_5_framework.dir/build.make:90: CMakeFiles/tu_5_framework.dir/5_framework/tests/example2.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:485: CMakeFiles/tu_5_framework.dir/all] Error 2 make: *** [Makefile:91: all] Error 2 How to fix this code ? Thanks for your reply.

Regards,

XS