在MSVC STL中,实现std::apply如下:
template <class _Callable, _Tuple_like _Tuple, size_t... _Indices>
constexpr decltype(auto) _Apply_impl(_Callable&& _Obj, _Tuple&& _Tpl, index_sequence<_Indices...>) noexcept(/* [...] */) {
return _STD invoke(_STD forward<_Callable>(_Obj), _STD get<_Indices>(_STD forward<_Tuple>(_Tpl))...);
}
Run Code Online (Sandbox Code Playgroud)
在表达式 中_STD get<_Indices>(_STD forward<_Tuple>(_Tpl))...,std::get被调用的次数与元组的长度相同。但是,每次调用 时std::get,都会有一个相应的调用std::forward.当转发右值时,std::forward相当于,意味着对该对象的std::move多次调用。这似乎可能是无效的。有人可以帮助解释我的担忧吗?std::move_Tpl
我尝试搜索各种资源,但找不到我正在寻找的答案。从定义开始也没有说服我。