The noexcept
specification on move-constructors are known to have performance implications in C++0x. For example, std::vector<T>::resize
, std::vector<T>::reserve
may use a non-throwing move-constructor of T if it can be proven to not throw. The noexcept
opeator is the way to check that property at compile-time. If noexcept says that T's move-ctor does not throw, T objects will be moved instead of copied most likely achieving higher performance.
My question is about member swap or namespace-level swap for a user-defined class T. C++0x spec spends some effort in exporting noexcept specifications of std::pair
, std::tuple
, std::array:swap
possibly indicating that user-defined classes should try to use the same principle. For instance, std::pair::swap
is declared as equivalent to:
vo开发者_StackOverflow中文版id std::pair::swap(pair& p) noexcept(noexcept(swap(first, p.first)) && noexcept(swap(second, p.second));
It basically says that pair's swap will throw if either swapping of first
or second
member throws. Swap of first
, second
possibly have their own noexcept specifications in terms of their members.
Finally the question: Are there generic algorithms out there (in stl or otherwise) that depending upon the noexcept specification of swap do different things? Moreover, is there a performance implication of that?
In addition to code that specifically behaves differently when something is noexcept (such as std::vector
), declaring a function noexcept can allow the compiler to do its own optimizations. At the very least the compiler doesn't have to keep track of certain things that are involved in exception handling, which might free up registers or execute less instructions among other things.
精彩评论