Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best implementation of the missing unary std::copy

C++11 introduces semantics to avoid unnecessarily copying objects, and std::move to apply those semantics when otherwise a copy would occur. However, there are now also some cases where a copy is required, but not the default.

Consider this naive implementation of reverse, for example. Because range-based for uses perfect forwarding, modification to the container within the loop amounts to corruption.

auto out_iter = container.rbegin();
for ( auto value : container ) {
    * out_iter ++ = value;
}

The goal is to fix this using

for ( auto value : copy( container ) ) {

It seems simple enough… accept any argument, get the underlying type and return a temporary copy.

like image 864
Potatoswatter Avatar asked Feb 23 '23 00:02

Potatoswatter


1 Answers

The return type you're computing is better known as typename std::decay<T>::type. Other than that, use std::forward in the body to reap the full benefits of perfect forwarding:

template<typename T>
typename std::decay<T>::type
val(T&& t)
{ return std::forward<T>(t); }
like image 136
Luc Danton Avatar answered Mar 06 '23 18:03

Luc Danton