[英]Can I use template parameter as argument to non-template method?
我希望将模板方法中的某些代码移至非模板方法中,以减小二进制大小。
有一个名为“ Target”的模板类,如下所示
template<TargetType K, typename V = plat_target_handle_t>
class Target
{
.............
..............
};
TargetType是枚举数据类型。
template<>
template< TargetType T>
std::vector<Target<T> >
Target<TARGET_TYPE_X>::getChildren(const TargetState i_state) const
{
std::vector<Target<T> > l_children;
for ( int i=0; i < elements_in_some_list ; ++i)
{
/*If the current entry in some_list match my critera, add to the l_children */
}
}
TargetType是枚举数据类型,而TARGET_TYPE_X是枚举值之一。
我想将选择子项的所有逻辑移到全局方法中,比如说getChildrenHelper。
getChildrenHelper的声明如下。
void getGhildrenHelper(const TargetType i_targetType,
const TargetState i_targetstate,
std::vector<Target<TARGET_TYPE_ALL>> & io_children);
然后,getChildren方法最终看起来像
template<>
template< TargetType T>
std::vector<Target<T> >
Target<TARGET_TYPE_X>::getChildren(const TargetState i_state) const
{
std::vector<Target<T> > l_children;
childHelper(T,i_state,l_children);
return l_children;
}
我的猜测是,尽管我正在使用的本机编译器未通过错误,但无法完成。
但是,在另一个现有代码中,类似的概念也可以很好地工作
template< TargetType K >
inline ReturnCode putParam(const Target<K>& i_target,
const RingID i_ringID,
const RingMode i_ringMode)
{
ReturnCode l_rc = FAPI2_RC_SUCCESS;
// Find the string in the SEEPROM
l_rc = findInImageAndApply(i_target, i_ringID, i_ringMode);
return l_rc;
}
fapi2::ReturnCode findImageAndApply(
const fapi2::Target<fapi2::TARGET_TYPE_ALL>& i_target,
const RingID i_ringID,
const fapi2::RingMode i_ringMode)
{
................
................
}
模板函数调用普通的非模板函数以执行不需要或不使用任何模板参数的大量代码是很常见的。 这是避免模板生成的代码膨胀的常用技术。
在您的情况下, TargetType
出现是模板参数,并且没有此类。 因此:
void getGhildrenHelper(const TargetType i_targetType,
const TargetState i_targetstate,
std::vector<Target<TARGET_TYPE_ALL>> & io_children);
本身不应该编译,因为TargetType
似乎是模板参数,而不是类名称(基于模板专业化中的代码)。
但是,您的代码可能在这里不明确。 无论如何,如果TargetType
和TargetState
以及Target<TARGET_TYPE_ALL>
都不是模板参数,则这将使其成为一个普通函数,并且可以肯定地从具有匹配参数的模板函数中调用它。
模板函数可以执行普通函数所做的任何事情,包括调用其他函数或使用其他模板。 要求与其他任何功能相同:匹配功能参数类型等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.