微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用boost :: di制作单例时如何传递构造函数参数

如何解决使用boost :: di制作单例时如何传递构造函数参数

我是依赖注入框架的新手。 在下面的代码中,当我尝试使Child对象成为单例对象时,它将产生错误并且无法编译(请参见下面的代码)。

    In file included from /root/Projects/CLionProjects/Experiment/DI/main.cpp:2:
/usr/local/include/boost/di.hpp: In instantiation of ‘struct boost::ext::di::v1_2_0::aux::concept_check<boost::ext::di::v1_2_0::concepts::scope<Child>::requires_<boost::ext::di::v1_2_0::concepts::scope<boost::ext::di::v1_2_0::_,boost::ext::di::v1_2_0::_>::is_referable,boost::ext::di::v1_2_0::concepts::scope<boost::ext::di::v1_2_0::_,boost::ext::di::v1_2_0::_>::try_create,boost::ext::di::v1_2_0::_>::create> >’:
/usr/local/include/boost/di.hpp:1710:71:   required by substitution of ‘template<class T,typename boost::ext::di::v1_2_0::aux::concept_check<typename boost::ext::di::v1_2_0::concepts::scopable__<T>::type>::type <anonymous> > auto boost::ext::di::v1_2_0::core::dependency<boost::ext::di::v1_2_0::scopes::deduce,Child,boost::ext::di::v1_2_0::no_name,void,boost::ext::di::v1_2_0::core::pool<boost::ext::di::v1_2_0::aux::type_list<boost::ext::di::v1_2_0::core::ctor_arg<Child,int&>,boost::ext::di::v1_2_0::core::ctor_arg<Child,1,double> > > >::in<T,<anonymous> >(const T&) [with T = Child; typename boost::ext::di::v1_2_0::aux::concept_check<typename boost::ext::di::v1_2_0::concepts::scopable__<T>::type>::type <anonymous> = <missing>]’
/root/Projects/CLionProjects/Experiment/DI/main.cpp:118:68:   required from here
/usr/local/include/boost/di.hpp:379:20: error: static assertion Failed: constraint not satisfied
  379 |   static_assert(T::value,"constraint not satisfied");
      |                    ^~~~~
/root/Projects/CLionProjects/Experiment/DI/main.cpp: In function ‘auto Singleton::retobjChild()’:
/root/Projects/CLionProjects/Experiment/DI/main.cpp:118:68: error: no matching function for call to ‘boost::ext::di::v1_2_0::core::dependency<boost::ext::di::v1_2_0::scopes::deduce,double> > > >::in<Child>(const boost::ext::di::v1_2_0::scopes::singleton&)’
  118 |         auto secondVariable = firstvariable.in<Child>(di::singleton);
      |                                                                    ^
In file included from /root/Projects/CLionProjects/Experiment/DI/main.cpp:2:
/usr/local/include/boost/di.hpp:1711:8: note: candidate: ‘template<class T,typename boost::ext::di::v1_2_0::aux::concept_check<typename boost::ext::di::v1_2_0::concepts::scopable__<T>::type>::type <anonymous> > auto boost::ext::di::v1_2_0::core::dependency< <template-parameter-1-1>,TExpected,<template-parameter-1-3>,<template-parameter-1-4>,<template-parameter-1-5>,<template-parameter-1-6> >::in(const T&) [with T = T; typename boost::ext::di::v1_2_0::aux::concept_check<typename boost::ext::di::v1_2_0::concepts::scopable__<T>::type>::type <anonymous> = <anonymous>; TScope = boost::ext::di::v1_2_0::scopes::deduce; TExpected = Child; TGiven = Child; TName = boost::ext::di::v1_2_0::no_name; TPriority = void; TCtor = boost::ext::di::v1_2_0::core::pool<boost::ext::di::v1_2_0::aux::type_list<boost::ext::di::v1_2_0::core::ctor_arg<Child,double> > >]’
 1711 |   auto in(const T&)noexcept {
      |        ^~
/usr/local/include/boost/di.hpp:1711:8: note:   substitution of deduced template arguments resulted in errors seen above
/root/Projects/CLionProjects/Experiment/DI/main.cpp:121:37: error: expected primary-expression before ‘>’ token
  121 |         return injector.create<Child>();
      |                                     ^
/root/Projects/CLionProjects/Experiment/DI/main.cpp:121:39: error: expected primary-expression before ‘)’ token
  121 |         return injector.create<Child>();

但是,如果我不使用单例,则代码可以正常工作(请参见下面的代码)。 我在这代码中做错什么了?请解释。

如果我不传递参数,则可以正确创建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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?