简体   繁体   中英

Can I use template parameter as argument to non-template method?

I am looking to move some of the code of within a template method to a non-template method in order to decrease the binary size.

There is a template class called 'Target', as illustrated below

template<TargetType K, typename V = plat_target_handle_t>
class Target
{
   .............
   ..............
};

TargetType is an enum data type.

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 is an enum data type and TARGET_TYPE_X is one of the enum values.

I want to move all the logic to select the children to a global method, lets say getChildrenHelper.

getChildrenHelper is declared as below.

void  getGhildrenHelper(const TargetType i_targetType,
      const TargetState i_targetstate,
     std::vector<Target<TARGET_TYPE_ALL>> & io_children);

And then the getChildren method would eventually look like

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;
}

My guess is this cannot be done, though the native compiler that I am working with did not through an error.

However there is another existing code where the similar concept is working perfectly fine

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)
{
 ................
................
}

It is quite common for template functions to invoke ordinary, non-template functions in order to execute a large chunk of code that does not need or use any template parameters. This is a common technique for avoiding template-generated code bloat.

In your case, TargetType appears is a template parameter, and there is no such class. As such:

void  getGhildrenHelper(const TargetType i_targetType,
      const TargetState i_targetstate,
     std::vector<Target<TARGET_TYPE_ALL>> & io_children);

that by itself should not compile, since TargetType appears to be a template parameter, and not a class name, based on the code in your template specialization.

But, your code might be ambiguous, here. In any case, if neither TargetType and TargetState , nor Target<TARGET_TYPE_ALL> are template parameters, this would make this an ordinary function, and it can certainly be invoked from a template function, with matching parameters.

A template function can do anything that an ordinary function does, including calling other functions, or using other templates. The requirements are the same as for any other function: matching function parameter types, etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM