如何在模板方法中使用 std::forward,例如 std::array<std::array> 中只有一个元素被传递给另一个模板方法
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
假设我们有以下简化场景,其中模板化方法调用模板化方法如下:
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;
}
有了通用参考,我知道我应该 std::forward
。如果我需要整个 tb
,那么我会 auto temp = fa(std::forward<Tb>(tb))
。但是我不清楚如何通过 tb[n][m]
,因为我只通过了 tb
的一个条目(条目中的一个条目)。他们是进行这种转发的方法吗?
假设您没有尝试做任何异常的事情,您可以转发参数并在转发的参数上调用 operator[]
。将选择正确的成员函数重载。
auto temp = fa( std::forward<Tb>( tb )[n][m] );
假设我们有以下简化场景,其中模板化方法调用模板化方法如下:
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;
}
有了通用参考,我知道我应该 std::forward
。如果我需要整个 tb
,那么我会 auto temp = fa(std::forward<Tb>(tb))
。但是我不清楚如何通过 tb[n][m]
,因为我只通过了 tb
的一个条目(条目中的一个条目)。他们是进行这种转发的方法吗?
假设您没有尝试做任何异常的事情,您可以转发参数并在转发的参数上调用 operator[]
。将选择正确的成员函数重载。
auto temp = fa( std::forward<Tb>( tb )[n][m] );