如何解决使用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 举报,一经查实,本站将立刻删除。