C++ 如何在使用boost::di生成singleton时传递构造函数参数

C++ 如何在使用boost::di生成singleton时传递构造函数参数,c++,boost,dependency-injection,C++,Boost,Dependency Injection,我不熟悉依赖注入框架。 在下面的代码中,当我尝试将子对象设置为singleton时,它会给出错误并且不会编译(请参阅下面的代码) 包含在/root/Projects/CLionProjects/experience/DI/main.cpp:2中的文件中: /usr/local/include/boost/di.hpp:在“struct boost::ext::di::v1_2_0::aux::concept_check”的实例化中: /usr/local/include/boost/di.hpp

我不熟悉依赖注入框架。 在下面的代码中,当我尝试将子对象设置为singleton时,它会给出错误并且不会编译(请参阅下面的代码)

包含在/root/Projects/CLionProjects/experience/DI/main.cpp:2中的文件中:
/usr/local/include/boost/di.hpp:在“struct boost::ext::di::v1_2_0::aux::concept_check”的实例化中:
/usr/local/include/boost/di.hpp:1710:71:替换“模板自动boost::ext::di::v1_2_0::core::dependency::in(const T&)[with T=Child;typename boost::ext::di::v1_2_0::aux::concept\u check::type=]
/root/Projects/CLionProjects/experience/DI/main.cpp:118:68:此处为必填项
/usr/local/include/boost/di.hpp:379:20:错误:静态断言失败:约束未满足
379 | static|u assert(T::value,“约束未满足”);
|                    ^~~~~
/root/Projects/CLionProjects/experience/DI/main.cpp:在函数“auto Singleton::retObjChild()”中:
/root/Projects/CLionProjects/experience/DI/main.cpp:118:68:错误:对“boost::ext::DI::v1_2_0::core::dependency::in(const boost::ext::DI::v1_2_0::scopes::singleton&)”的调用没有匹配函数
118 | auto secondVariable=firstVariable.in(di::singleton);
|                                                                    ^
在/root/Projects/CLionProjects/experience/DI/main.cpp中包含的文件中:2:
/usr/local/include/boost/di.hpp:1711:8:注意:候选:“模板自动增强::ext::di::v1_2_0::核心::依赖项<,TExpected,,>::in(const T&)[with T=T;typename boost::ext::di::v1_2_0::aux::concept_check::type=;TScope=boost::ext::di::v1_2_0::scopes::演绎;TExpected=Child;TGiven=Child;TName=boost::ext::ext::di::v1_2_0::no_name;TPriority=void;TCtor=boost::di::v1_2_0::core::pool]'
1711 |自动输入(常数T&)无例外{
|        ^~
/usr/local/include/boost/di.hpp:1711:8:注意:替换推导的模板参数会导致上述错误
/root/Projects/CLionProjects/experience/DI/main.cpp:121:37:错误:在“>”标记之前应该有主表达式
121 |返回喷油器。创建();
|                                     ^
/root/Projects/CLionProjects/experience/DI/main.cpp:121:39:错误:在“')标记之前应该有主表达式
121 |返回喷油器。创建();
但如果我不使用singleton,代码将正常工作(请参阅下面的代码)。 我在这个代码中做错了什么?请解释

另外,如果我不传递参数,我可以正确地创建单例

#include "boost/di.hpp"
#include <iostream>
#include <thread>
#include <chrono>

using namespace std;
using namespace boost;
using namespace chrono_literals;

class Base
{
    int k;

    protected:
        Base(int param) : k{param}{}

        virtual void printFields()
        {
            cout << "k = " << k << endl;
        }
};

class Child final : public Base
{
    double j;

    public:
        Child(int param, double d) : Base(param), j {d} {}

        void printFields() override
        {
            cout << "j = " << j << endl;
            Base::printFields();
        }
};

int globalK = 0;

void setGlobalK()
{
    int count{};
    while(count++ < 2)
    {
        this_thread::sleep_for(1s);
    }
    globalK = 100;
}

namespace Singleton
{
    auto retObjChild()
    {
        while(globalK != 100) this_thread::sleep_for(1s);

        // Trying to make it singleton is not working,
        // auto injector = di::make_injector(di::bind<Child>.to<Child>(globalK, 5.6).in<Child>(di::singleton));

        // But this works
        // auto injector = di::make_injector(di::bind<Child>.to<Child>(globalK, 5.6));

        // Zig Razor's suggestion (does not work). Content updated to reflect Error in following code
        auto firstVariable = di::bind<Child>.to<Child>(globalK, 5.6);
        auto secondVariable = firstVariable.in<Child>(di::singleton);
        auto injector = di::make_injector(secondVariable);
        return injector.create<Child>();
    }
}

int main()
{
    thread t{setGlobalK};
    Singleton::retObjChild().printFields();
    t.join();
    return 0;
}
#包括“boost/di.hpp”
#包括
#包括
#包括
使用名称空间std;
使用名称空间boost;
使用名称空间chrono_文本;
阶级基础
{
int k;
受保护的:
基(int参数):k{param}{}
虚拟空打印字段()
{

cout
给出错误并且不编译
-什么错误?请添加编译错误。@ZigRazor Raffallo更新了错误boost中没有di库。@user7860670耶,我认为它是的一部分,不管是什么
#include "boost/di.hpp"
#include <iostream>
#include <thread>
#include <chrono>

using namespace std;
using namespace boost;
using namespace chrono_literals;

class Base
{
    int k;

    protected:
        Base(int param) : k{param}{}

        virtual void printFields()
        {
            cout << "k = " << k << endl;
        }
};

class Child final : public Base
{
    double j;

    public:
        Child(int param, double d) : Base(param), j {d} {}

        void printFields() override
        {
            cout << "j = " << j << endl;
            Base::printFields();
        }
};

int globalK = 0;

void setGlobalK()
{
    int count{};
    while(count++ < 2)
    {
        this_thread::sleep_for(1s);
    }
    globalK = 100;
}

namespace Singleton
{
    auto retObjChild()
    {
        while(globalK != 100) this_thread::sleep_for(1s);

        // Trying to make it singleton is not working,
        // auto injector = di::make_injector(di::bind<Child>.to<Child>(globalK, 5.6).in<Child>(di::singleton));

        // But this works
        // auto injector = di::make_injector(di::bind<Child>.to<Child>(globalK, 5.6));

        // Zig Razor's suggestion (does not work). Content updated to reflect Error in following code
        auto firstVariable = di::bind<Child>.to<Child>(globalK, 5.6);
        auto secondVariable = firstVariable.in<Child>(di::singleton);
        auto injector = di::make_injector(secondVariable);
        return injector.create<Child>();
    }
}

int main()
{
    thread t{setGlobalK};
    Singleton::retObjChild().printFields();
    t.join();
    return 0;
}