Does any swap specification have a performance impact?

The noexcept specification for move constructors is noexcept to have performance values ​​in C ++ 0x. For example, std::vector<T>::resize , std::vector<T>::reserve can use the T non-throwing constructor if it can be proved that it does not throw. The noexcept is a way to check this property at compile time. If noexcept says that T move-ctor is not throwing, T's objects will be moved, not copied, more likely to achieve better performance.

My question is about exchanging members or exchanging a namespace for a user class T. The C ++ 0x spec takes some effort to export the noexcept specifications std::pair , std::tuple , std::array:swap , possibly indicating that user Certain classes should try to use the same principle. For example, std::pair::swap declared equivalent:

 void std::pair::swap(pair& p) noexcept(noexcept(swap(first, p.first)) && noexcept(swap(second, p.second)); 

It is basically said that the exchange of pairs will depend if either the change of elements is first or second . Swap first , second may have their own noexcept specifications in terms of their members.

Finally, the question is: are there general algorithms (in stl or otherwise) that do different things depending on the noexcept swap specification? Also, are there any implications for this?

+4
source share
1 answer

In addition to code that specifically behaves differently when something doesn't exist (like std::vector ), declaring a noexcept function can allow the compiler to perform its own optimizations. At the very least, the compiler should not keep track of certain things that are involved in exception handling, which can free the registers or execute fewer instructions by the way.

+3
source

All Articles