【发布时间】:2020-04-04 22:15:33
【问题描述】:
假设我们有以下简化的场景,其中模板化方法调用模板化方法如下:
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 的一个条目(一个条目)。他们有办法进行这种转发吗?
【问题讨论】:
-
看起来您正在制作元素的本地副本。故意的?
-
@ZuodianHu 你的意思是
auto temp = fa(tb[n][m])那一行吗? -
没关系,你将它传递给函数。
标签: c++ templates forwarding-reference