C++ 如何在一个构造函数中使用两个可变模板参数来绑定两个函数?
我想构造一个对象,该对象传递两个具有可变参数数的函数:C++ 如何在一个构造函数中使用两个可变模板参数来绑定两个函数?,c++,templates,C++,Templates,我想构造一个对象,该对象传递两个具有可变参数数的函数: class PhaseSetControlProperty : public ControlProperty { public: template<typename FuncKicked, typename... ArgsKicked, typename FuncActivated, typename... ArgsActivated> PhaseSetControlProperty(const std::
class PhaseSetControlProperty : public ControlProperty {
public:
template<typename FuncKicked, typename... ArgsKicked, typename FuncActivated, typename... ArgsActivated>
PhaseSetControlProperty(const std::vector<std::string> &kickedPhaseNames, const std::vector<std::string> &activatedPhaseNames, FuncKicked funcKicked, ArgsKicked ...argsKicked, FuncActivated funcActivated, ArgsActivated... argsActivated): ControlProperty("PhaseSetControl")
{
getModel()->addProperty(new StringListProperty("kickedPhaseNames", kickedPhaseNames));
getModel()->addProperty(new StringListProperty("activePhaseNames", activatedPhaseNames));
getModel()->addProperty(new StringListProperty("activated", activatedPhaseNames, funcActivated, argsActivated...));
getModel()->addProperty(new StringListProperty("kicked", kickedPhaseNames, funcKicked, argsKicked...));
}
};
this和std::placeholder::_1是可变参数
以前的解决方案是这样的:
model->addProperty(new PhaseSetControlProperty(kickedphasenames, activephasenames, &BDlines::kickedPhasesFunctionCallback, this, std::placeholders::_1, &BDlines::activePhasesFunctionCallback, this, std::placeholders::_1));
kicked_property = new FunctionProperty("kicked", &BDlines::kickedPhasesFunctionCallback, this, std::placeholders::_1);
activated_property = new FunctionProperty("activated", &BDlines::activePhasesFunctionCallback, this, std::placeholders::_1);
<> C++如何知道哪些参数属于哪些模板参数?如何以简单的方式实现这一点
我目前的解决方案是使用两个附加函数,分别添加属性“activated”和属性“kicked”,基本上与旧版本类似,但我只想使用一个调用
< C++如何知道哪些参数属于哪个模板参数?< /p>
没有。这就是为什么模板参数包只有在它们是最后一个参数时才是可推断的
如何以简单的方式实现这一点
如果每一组参数仅用于构造属性,则只需获取属性的参数包:
model->addProperty(new PhaseSetControlProperty(kickedphasenames, activephasenames,
new StringListProperty (&BDlines::kickedPhasesFunctionCallback, this, std::placeholders::_1),
new StringListProperty (&BDlines::activePhasesFunctionCallback, this, std::placeholders::_1)));
它还具有支持其他种类和数量的房地产的额外好处
< C++如何知道哪些参数属于哪个模板参数?< /p>
没有。这就是为什么模板参数包只有在它们是最后一个参数时才是可推断的
如何以简单的方式实现这一点
如果每一组参数仅用于构造属性,则只需获取属性的参数包:
model->addProperty(new PhaseSetControlProperty(kickedphasenames, activephasenames,
new StringListProperty (&BDlines::kickedPhasesFunctionCallback, this, std::placeholders::_1),
new StringListProperty (&BDlines::activePhasesFunctionCallback, this, std::placeholders::_1)));
还具有支持其他类型和数量的属性的附加好处。不确定我是否完全理解了您的问题,但您能否将函子作为模板参数传递,而不是单独传递函数和参数?因此,您的代码可能如下所示:
template<typename FuncKickedFunctor, typename FuncActivatedFunctor>
PhaseSetControlProperty(const std::vector<std::string> &kickedPhaseNames, const std::vector<std::string> &activatedPhaseNames, FuncKickedFunctor funcKickedFunctor, FuncActivatedFunctor funcActivatedFunctor): ControlProperty("PhaseSetControl")
{
getModel()->addProperty(new StringListProperty("kickedPhaseNames", kickedPhaseNames));
getModel()->addProperty(new StringListProperty("activePhaseNames", activatedPhaseNames));
getModel()->addProperty(new StringListProperty("activated", activatedPhaseNames, funcActivatedFunctor));
getModel()->addProperty(new StringListProperty("kicked", kickedPhaseNames, funcKickedFunctor));
}
模板
PhaseSetControl属性(常数std::vector&kickedPhaseNames,常数std::vector&activatedPhaseNames,FuncKickedFunctor FuncKickedFunctor,FuncActivatedFunctor FuncActivatedFunctor):控制属性(“PhaseSetControl”)
{
getModel()->addProperty(新的StringListProperty(“kickedPhaseNames”,kickedPhaseNames));
getModel()->addProperty(新的StringListProperty(“activePhaseNames”,activatedPhaseNames));
getModel()->addProperty(新StringListProperty(“已激活”,activatedPhaseNames,FunctionActivatedFunctor));
getModel()->addProperty(新的StringListProperty(“kicked”,kickedPhaseNames,funcKickedFunctor));
}
我不确定我是否完全理解了你的问题,但你能不能将函子作为模板参数传递,而不是单独传递函数和参数?因此,您的代码可能如下所示:
template<typename FuncKickedFunctor, typename FuncActivatedFunctor>
PhaseSetControlProperty(const std::vector<std::string> &kickedPhaseNames, const std::vector<std::string> &activatedPhaseNames, FuncKickedFunctor funcKickedFunctor, FuncActivatedFunctor funcActivatedFunctor): ControlProperty("PhaseSetControl")
{
getModel()->addProperty(new StringListProperty("kickedPhaseNames", kickedPhaseNames));
getModel()->addProperty(new StringListProperty("activePhaseNames", activatedPhaseNames));
getModel()->addProperty(new StringListProperty("activated", activatedPhaseNames, funcActivatedFunctor));
getModel()->addProperty(new StringListProperty("kicked", kickedPhaseNames, funcKickedFunctor));
}
模板
PhaseSetControl属性(常数std::vector&kickedPhaseNames,常数std::vector&activatedPhaseNames,FuncKickedFunctor FuncKickedFunctor,FuncActivatedFunctor FuncActivatedFunctor):控制属性(“PhaseSetControl”)
{
getModel()->addProperty(新的StringListProperty(“kickedPhaseNames”,kickedPhaseNames));
getModel()->addProperty(新的StringListProperty(“activePhaseNames”,activatedPhaseNames));
getModel()->addProperty(新StringListProperty(“已激活”,activatedPhaseNames,FunctionActivatedFunctor));
getModel()->addProperty(新的StringListProperty(“kicked”,kickedPhaseNames,funcKickedFunctor));
}
您可以使用std::tuple
收集参数-例如
template <typename... Args1, typename... Args2>
void foo(const tuple<Args1...>& t1, const tuple<Args2...>& t2) {
cout << tuple_size<tuple<Args1...>>::value << ' ' << tuple_size<tuple<Args2...>>::value << endl;
}
模板
void foo(常量元组和t1、常量元组和t2){
cout您可以使用std::tuple
来收集参数,例如
template <typename... Args1, typename... Args2>
void foo(const tuple<Args1...>& t1, const tuple<Args2...>& t2) {
cout << tuple_size<tuple<Args1...>>::value << ' ' << tuple_size<tuple<Args2...>>::value << endl;
}
模板
void foo(常量元组和t1、常量元组和t2){
请不要断行,以免问题的重要部分隐藏在滚动条后面。请断行,以免问题的重要部分隐藏在滚动条后面。