Search code examples
c++templatesforwarding-reference

How to use std::forward in a template method, when only one element of, for example std::array<std::array>, is passed to another template method


Lets say we have the following simplified scenario where a templated method calls a templated method as follows:

template<typename Ta>
inline auto fa(Ta&& ta){  
myclassA ra;
// doing things to "ra" based on "ta"...
return ra
}

template<typename Tb>
inline auto fb(Tb&& tb){  
myclassB rb;
// doing things to "rb" based on "tb"...
// at some point:
auto temp = fa(tb[n][m]) //should this not be std::forward? how do you do that? 
// doing things to "rb" based on "temp"...
return rb;
}

With universal references I am aware I am supposed to std::forward. IF I needed to pas tb as a whole then I would do auto temp = fa(std::forward<Tb>(tb)). HOWEVER It is unclear to me how to pass tb[n][m], since I am passing only one entry (of an entry) of tb. Is their a way to do this forwarding?


Solution

  • Assuming you're not trying to do anything to unusual, you can just forward the parameter and call operator[] on the forwarded parameter. The correct member function overloads will be selected.

    auto temp = fa( std::forward<Tb>( tb )[n][m] );