在我的回答中here https://stackoverflow.com/a/74078452/11998382,巴里指出最好打电话views::transform(&Planter::getPlants)
因为views::transform([](Planter const& planter){...
不小心复制了
#if 1
auto plants = planters
| std::views::transform([](Planter const& planter){ return planter.getPlants();})
| std::views::join
| std::views::common
;
// Plant copy constructor
// Plant copy constructor
// Plant copy constructor
// Plant copy constructor
// Plant copy constructor
#else
auto plants = planters
| std::views::transform(&Planter::getPlants)
| std::views::join
;
#endif
// Plant copy constructor
// Plant copy constructor
Here Plant
是一个包装器int
and Planter
是一个包装器std::vector<int>
.
https://godbolt.org/z/dr7PM5Tvd https://godbolt.org/z/dr7PM5Tvd
哦,这个我还真认识。推导的 lambda 返回类型实际上衰减了 const ref 限定符getPlants
.
您可以通过将 lambda 的返回类型声明为来解决此问题decltype(auto)
views::transform([](Planter const& planter) -> decltype(auto){...});
https://godbolt.org/z/ocK5PG1z1 https://godbolt.org/z/ocK5PG1z1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)