c++ - Why do std::future<T> and std::shared_future<T> not provide member swap()? -


all kinds of classes in c++ standard library have member swap function, including polymorphic classes std::basic_ios<chart>. template class std::shared_future<t> value type , std::future<t> move-only value type. there particular reason, don't provide swap() member function?

member swap massive performance increase prior std::move support in c++11. way move 1 vector spot, example. used in vector resizes well, , meant inserting vector of vectors not complete performance suicide.

after std::move arrived in c++11, many sometimes-empty types default implementation of std::swap:

template<class t> void swap( t& lhs, t& rhs ) {   auto tmp = std::move(rhs);   rhs = std::move(lhs);   lhs = std::move(tmp); } 

is going fast custom-written one.

existing types swap members unlikely lose them (at least immediately). extending api of new type should, however, justified.

if std::future wrapper around std::unique_ptr< future_impl >, above going require 4 pointer reads, 3 pointer writes, , 1 branch. , optimizating compiler inlined it1 reduce down 2 pointer reads , 2 pointer writes (using ssa2 example), optimized .swap member function do.


1 knows intermediate access lhs , rhs never occurs, existence of tmp can eliminated as-if once proves tmp empty , hence has no-op dtor.

2 static single assignment, break program down such every assignment primitive creates brand new variable (with metadata). prove properties variable, , eliminate redundant ones.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -