Modern C++ std::swap的实现原理

前言

之前我们研究了std::bind, std::move的实现原理,如有兴趣可以往前翻翻。本节讨论下std::swap, 也比较简单。

实现代码

/usr/include/c++/8/bits/move.h

158 #define _GLIBCXX_MOVE(__val) std::move(__val)

178 #if __cplusplus >= 201103L
179     typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>,
180       is_move_constructible<_Tp>,
181       is_move_assignable<_Tp>>::value>::type
182     swap(_Tp& __a, _Tp& __b)
183     noexcept(__and_<is_nothrow_move_constructible<_Tp>,
184             is_nothrow_move_assignable<_Tp>>::value)
185 #else
186     void
187     swap(_Tp& __a, _Tp& __b)
188 #endif
189     {
190       // concept requirements
191       __glibcxx_function_requires(_SGIAssi
### C++ Standard Library Utilities Functions The C++ Standard Library includes a variety of utility functions and tools designed to simplify programming tasks by providing reusable code components. These utilities are part of the `<utility>` header file, which contains definitions for general-purpose templates that provide support for pairs, tuples, move semantics, type traits, and more. #### Common Utility Components 1. **std::pair**: This template class represents a pair of values as one object. It allows storing two heterogeneous objects within a single unit. ```cpp std::pair<int, double> myPair(10, 3.14); int firstValue = myPair.first; // Accessing the first element double secondValue = myPair.second; // Accessing the second element ``` 2. **std::tuple**: A tuple is an extension of `std::pair` allowing multiple elements with different types stored together in a single structure[^1]. ```cpp std::tuple<int, double, std::string> myTuple(42, 3.14, "hello"); int value1 = std::get<0>(myTuple); // Retrieve the integer component double value2 = std::get<1>(myTuple); // Retrieve the double component std::string value3 = std::get<2>(myTuple); // Retrieve the string component ``` 3. **Move Semantics (`std::move`, `std::forward`)**: Introduced in C++11, these utilities enable efficient transfer of resources between objects without unnecessary copying operations[^2]. They play a critical role in optimizing performance when dealing with large data structures such as strings or vectors. ```cpp std::vector<int> sourceVector{1, 2, 3}; std::vector<int> destinationVector = std::move(sourceVector); // Transfers ownership instead of copying ``` 4. **Type Traits**: Type traits allow querying properties about types at compile time, enabling conditional compilation based on those queries. Examples include checking whether a given type is integral, floating-point, pointer, etc. ```cpp static_assert(std::is_integral_v<int>, "int must be an integral type."); ``` 5. **Swap Functionality**: Provides mechanisms for swapping contents efficiently across various containers like arrays, lists, maps, sets, among others. ```cpp std::list<int> myList1 {1, 2}, myList2 {3, 4}; myList1.swap(myList2); // Swaps content directly rather than reassigning each item individually ``` 6. **Exchange Operation**: Allows replacing existing variable's value while returning previous state simultaneously using function named exchange()[^1]. ```cpp int originalVal; int newValue = std::exchange(originalVal, 789); // Sets 'originalVal' to 789 & returns old value before assignment took place ``` These functionalities form just some examples illustrating how versatile yet powerful modern-day implementations provided through updated standards continue enhancing developer productivity significantly over traditional approaches available earlier versions only limited capabilities compared against current offerings today!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深山老宅

鸡蛋不错的话,要不要激励下母鸡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值