vault backup: 2022-09-26 18:19:43
Affected files: .obsidian/workspace 01. 個人/00. Informations/Datas.md 01. 個人/01. Daily/2018/2018-10-05(週五).md 01. 個人/01. Daily/2018/2018-10-06(週六).md 01. 個人/01. Daily/2018/2018-10-08(週一).md 01. 個人/01. Daily/2018/2018-10-12(週五).md 01. 個人/01. Daily/2018/2018-10-13(週六).md 01. 個人/01. Daily/2018/2018-11-30(週五).md 01. 個人/01. Daily/2018/2018-12-03(週一).md 01. 個人/01. Daily/2018/2018-12-07(週五).md 01. 個人/01. Daily/2019/2019-09-12(週四).md 01. 個人/01. Daily/2020/02/2020-02-07(週五).md 01. 個人/01. Daily/2020/02/2020-02-14(週五).md 01. 個人/01. Daily/2020/03/2020-03-06(週五).md 01. 個人/01. Daily/2020/03/2020-03-12(週四).md 01. 個人/01. Daily/2020/03/2020-03-22(週日).md 01. 個人/01. Daily/2020/03/2020-03-23(週一).md 01. 個人/01. Daily/2020/03/2020-03-24(週二).md 01. 個人/01. Daily/2020/03/2020-03-25(週三).md 01. 個人/01. Daily/2020/03/2020-03-29(週日).md 01. 個人/01. Daily/2020/04/2020-04-02(週四).md 01. 個人/01. Daily/2020/04/2020-04-15(週三).md 01. 個人/01. Daily/2020/04/2020-04-18(週六).md 01. 個人/01. Daily/2020/04/2020-04-19(週日).md 01. 個人/01. Daily/2020/04/2020-04-22(週三).md 01. 個人/01. Daily/2020/04/2020-04-27(週一).md 01. 個人/01. Daily/2020/05/2020-05-09(週六).md 01. 個人/01. Daily/2020/05/2020-05-10(週日).md 01. 個人/01. Daily/2020/05/2020-05-11(週一).md 01. 個人/01. Daily/2020/05/2020-05-13(週三).md 01. 個人/01. Daily/2020/05/2020-05-14(週四).md 01. 個人/01. Daily/2020/05/2020-05-15(週五).md 01. 個人/01. Daily/2020/05/2020-05-16(週六).md 01. 個人/01. Daily/2020/05/2020-05-17(週日).md 01. 個人/01. Daily/2020/05/2020-05-18(週一).md 01. 個人/01. Daily/2020/05/2020-05-19(週二).md 01. 個人/01. Daily/2020/05/2020-05-20(週三).md 01. 個人/01. Daily/2020/07/2020-07-10(週五).md 01. 個人/01. Daily/2020/07/2020-07-24(週三).md 01. 個人/01. Daily/2020/08/2020-08-13(週四).md 01. 個人/01. Daily/2020/09/2020-09-12(週六).md 01. 個人/01. Daily/2020/10/2020-10-06(週二).md 01. 個人/01. Daily/2020/10/2020-10-22(週四).md 01. 個人/01. Daily/2020/11/2020-11-10(週二).md 01. 個人/01. Daily/2020/12/2020-12-02(週三).md 01. 個人/01. Daily/2020/12/2020-12-03(Thu).md 01. 個人/01. Daily/2020/12/2020-12-04(Fri).md 01. 個人/01. Daily/2020/12/2020-12-05(Sat).md 01. 個人/01. Daily/2020/12/2020-12-06(Sun).md 01. 個人/01. Daily/2020/12/2020-12-09(Wed).md 01. 個人/01. Daily/2020/12/2020-12-10(Thu).md 01. 個人/01. Daily/2020/12/2020-12-11(Fri).md 01. 個人/01. Daily/2020/12/2020-12-12(Sat).md 01. 個人/01. Daily/2020/12/2020-12-13(Sun).md 01. 個人/01. Daily/2020/12/2020-12-16(Wed).md 01. 個人/01. Daily/2020/12/2020-12-17(Thu).md 01. 個人/01. Daily/2020/12/2020-12-18(Fri).md 01. 個人/01. Daily/2020/12/2020-12-19(Sat).md 01. 個人/01. Daily/2020/12/2020-12-21(Mon).md 01. 個人/01. Daily/2020/12/2020-12-22(Tue).md 01. 個人/01. Daily/2020/12/2020-12-24(Thu).md 01. 個人/01. Daily/2020/12/2020-12-25(Fri).md 01. 個人/01. Daily/2020/12/2020-12-26(Sat).md 01. 個人/01. Daily/2020/12/2020-12-27(Sun).md 01. 個人/01. Daily/2020/12/2020-12-30(Wed).md 01. 個人/01. Daily/2021/01/2021-01-06(Wed).md 01. 個人/01. Daily/2021/01/2021-01-09(Sat).md 01. 個人/01. Daily/2021/01/2021-01-10(Sun).md 01. 個人/01. Daily/2021/01/2021-01-11(Mon).md 01. 個人/01. Daily/2021/01/2021-01-13(Wed).md 01. 個人/01. Daily/2021/01/2021-01-14(Thu).md 01. 個人/01. Daily/2021/01/2021-01-15(Fri).md 01. 個人/01. Daily/2021/01/2021-01-16(Sat).md 01. 個人/01. Daily/2021/01/2021-01-17(Sun).md 01. 個人/01. Daily/2021/01/2021-01-18(Mon).md 01. 個人/01. Daily/2021/01/2021-01-21(Thu).md 01. 個人/01. Daily/2021/01/2021-01-22(Fri).md 01. 個人/01. Daily/2021/01/2021-01-23(Sat).md 01. 個人/01. Daily/2021/01/2021-01-24(Sun).md 01. 個人/01. Daily/2021/01/2021-01-25(Mon).md 01. 個人/01. Daily/2021/01/2021-01-26(Tue).md 01. 個人/01. Daily/2021/01/2021-01-27(Wed).md 01. 個人/01. Daily/2021/01/2021-01-28(Thu).md 01. 個人/01. Daily/2021/01/2021-01-30(Sat).md 01. 個人/01. Daily/2021/02/2021-02-01(Mon).md 01. 個人/01. Daily/2021/02/2021-02-02(Tue).md 01. 個人/01. Daily/2021/02/2021-02-03(Wed).md 01. 個人/01. Daily/2021/02/2021-02-04(Thu).md 01. 個人/01. Daily/2021/02/2021-02-08(Mon).md 01. 個人/01. Daily/2021/02/2021-02-13(Sat).md 01. 個人/01. Daily/2021/02/2021-02-15(Mon).md 01. 個人/01. Daily/2021/02/2021-02-16(Tue).md 01. 個人/01. Daily/2021/02/2021-02-20(Sat).md 01. 個人/01. Daily/2021/02/2021-02-21(Sun).md 01. 個人/01. Daily/2021/02/2021-02-22(Mon).md 01. 個人/01. Daily/2021/03/2021-03-01(Mon).md 01. 個人/01. Daily/2021/03/2021-03-13(週六).md 01. 個人/01. Daily/2021/03/2021-03-15(週一).md 01. 個人/01. Daily/2021/03/2021-03-17(週三).md 01. 個人/01. Daily/2021/03/2021-03-20(週六).md 01. 個人/01. Daily/2021/03/2021-03-21(週日).md 01. 個人/01. Daily/2021/03/2021-03-27(週六).md 01. 個人/01. Daily/2021/03/2021-03-28(週日).md 01. 個人/01. Daily/2021/04/2021-04-05(週一).md 01. 個人/01. Daily/2021/04/2021-04-06(週二).md 01. 個人/01. Daily/2021/04/2021-04-08(週四).md 01. 個人/01. Daily/2021/04/2021-04-10(週六).md 01. 個人/01. Daily/2021/04/2021-04-11(週日).md 01. 個人/01. Daily/2021/04/2021-04-12(週一).md 01. 個人/01. Daily/2021/05/2021-05-02(週日).md 01. 個人/01. Daily/2021/05/2021-05-22(週六).md 01. 個人/01. Daily/2021/05/2021-05-24(週一).md 01. 個人/01. Daily/2021/05/2021-05-25(週二).md 01. 個人/01. Daily/2021/05/2021-05-26(週三).md 01. 個人/01. Daily/2021/05/2021-05-27(週四).md 01. 個人/01. Daily/2021/05/2021-05-28(週五).md 01. 個人/01. Daily/2021/05/2021-05-29(週六).md 01. 個人/01. Daily/2021/06/2021-06-01(週二).md 01. 個人/01. Daily/2021/06/2021-06-02(週三).md 01. 個人/01. Daily/2021/06/2021-06-03(週四).md 01. 個人/01. Daily/2021/06/2021-06-04(週五).md 01. 個人/01. Daily/2021/06/2021-06-05(週六).md 01. 個人/01. Daily/2021/06/2021-06-06(週日).md 01. 個人/01. Daily/2021/06/2021-06-07(週一).md 01. 個人/01. Daily/2021/06/2021-06-08(週二).md 01. 個人/01. Daily/2021/06/2021-06-09(週三).md 01. 個人/01. Daily/2021/06/2021-06-10(週四).md 01. 個人/01. Daily/2021/06/2021-06-11(週五).md 01. 個人/01. Daily/2021/06/2021-06-12(週六).md 01. 個人/01. Daily/2021/06/2021-06-15(週二).md 01. 個人/01. Daily/2021/06/2021-06-26(週六).md 01. 個人/01. Daily/2021/06/2021-06-28(週一).md 01. 個人/01. Daily/2021/07/2021-07-01(週四).md 01. 個人/01. Daily/2021/07/2021-07-02(週五).md 01. 個人/01. Daily/2021/07/2021-07-04(週日).md 01. 個人/01. Daily/2021/07/2021-07-05(週一).md 01. 個人/01. Daily/2021/07/2021-07-06(週二).md 01. 個人/01. Daily/2021/07/2021-07-07(週三).md 01. 個人/01. Daily/2021/07/2021-07-09(週五).md 01. 個人/01. Daily/2021/07/2021-07-10(週六).md 01. 個人/01. Daily/2021/07/2021-07-11(週日).md 01. 個人/01. Daily/2021/07/2021-07-14(週三).md 01. 個人/01. Daily/2021/07/2021-07-15(週四).md 01. 個人/01. Daily/2021/07/2021-07-16(週五).md 01. 個人/01. Daily/2021/07/2021-07-17(週六).md 01. 個人/01. Daily/2021/07/2021-07-18(週日).md 01. 個人/01. Daily/2021/07/2021-07-19(週一).md 01. 個人/01. Daily/2021/07/2021-07-20(週二).md 01. 個人/01. Daily/2021/07/2021-07-21(週三).md 01. 個人/01. Daily/2021/07/2021-07-22(週四).md 01. 個人/01. Daily/2021/07/2021-07-23(週五).md 01. 個人/01. Daily/2021/07/2021-07-24(週六).md 01. 個人/01. Daily/2021/07/2021-07-25(週日).md 01. 個人/01. Daily/2021/07/2021-07-26(週一).md 01. 個人/01. Daily/2021/07/2021-07-27(週二).md 01. 個人/01. Daily/2021/07/2021-07-28(週三).md 01. 個人/01. Daily/2021/07/2021-07-29(週四).md 01. 個人/01. Daily/2021/07/2021-07-30(週五).md 01. 個人/01. Daily/2021/07/2021-07-31(週六).md 01. 個人/01. Daily/2021/08/2021-08-02(週一).md 01. 個人/01. Daily/2021/08/2021-08-03(週二).md 01. 個人/01. Daily/2021/08/2021-08-04(週三).md 01. 個人/01. Daily/2021/08/2021-08-05(週四).md 01. 個人/01. Daily/2021/08/2021-08-06(週五).md 01. 個人/01. Daily/2021/08/2021-08-07(週六).md 01. 個人/01. Daily/2021/08/2021-08-08(週日).md 01. 個人/01. Daily/2021/08/2021-08-09(週一).md 01. 個人/01. Daily/2021/08/2021-08-10(週二).md 01. 個人/01. Daily/2021/08/2021-08-11(週三).md 01. 個人/01. Daily/2021/08/2021-08-13(週五).md 01. 個人/01. Daily/2021/08/2021-08-14(週六).md 01. 個人/01. Daily/2021/08/2021-08-16(週一).md 01. 個人/01. Daily/2021/08/2021-08-17(週二).md 01. 個人/01. Daily/2021/08/2021-08-20(週五).md 01. 個人/01. Daily/2021/08/2021-08-24(週二).md 01. 個人/01. Daily/2021/08/2021-08-25(週三).md 01. 個人/01. Daily/2021/08/2021-08-26(週四).md 01. 個人/01. Daily/2021/09/2021-09-01(週三).md 01. 個人/01. Daily/2021/09/2021-09-02(週四).md 01. 個人/01. Daily/2021/09/2021-09-09(週四).md 01. 個人/01. Daily/2021/09/2021-09-13(週一).md 01. 個人/01. Daily/2021/09/2021-09-16(週四).md 01. 個人/01. Daily/2021/09/2021-09-22(週三).md 01. 個人/01. Daily/2021/09/2021-09-23(週四).md 01. 個人/01. Daily/2021/09/2021-09-24(週五).md 01. 個人/01. Daily/2021/09/2021-09-27(週一).md 01. 個人/01. Daily/2021/09/2021-09-28(週二).md 01. 個人/01. Daily/2021/10/2021-10-04(週一).md 01. 個人/01. Daily/2021/10/2021-10-06(週三).md 01. 個人/01. Daily/2021/10/2021-10-14(週四).md 01. 個人/01. Daily/2021/10/2021-10-20(週三).md 01. 個人/01. Daily/2021/10/2021-10-28(週四).md 01. 個人/01. Daily/2021/11/2021-11-01(週一).md 01. 個人/01. Daily/2021/11/2021-11-04(週四).md 01. 個人/01. Daily/2021/11/2021-11-12(週五).md 01. 個人/01. Daily/2021/11/2021-11-15(週一).md 01. 個人/01. Daily/2021/12/2021-12-01(週三).md 01. 個人/01. Daily/2021/12/2021-12-13(週一).md 01. 個人/01. Daily/2021/12/2021-12-15(週三).md 01. 個人/01. Daily/2021/12/2021-12-16(週四).md 01. 個人/01. Daily/2021/12/2021-12-17(週五).md 01. 個人/01. Daily/2021/12/2021-12-18(週六).md 01. 個人/01. Daily/2021/12/2021-12-20(週一).md 01. 個人/01. Daily/2021/12/2021-12-21(週二).md 01. 個人/01. Daily/2021/12/2021-12-23(週四).md 01. 個人/01. Daily/2021/12/2021-12-24(週五).md 01. 個人/01. Daily/2022-06-01(週三).md 01. 個人/01. Daily/2022-06-06(週一).md 01. 個人/01. Daily/2022-07-15(週五).md 01. 個人/01. Daily/2022/01/2022-01-01(週六).md 01. 個人/01. Daily/2022/01/2022-01-08(週六).md 01. 個人/01. Daily/2022/01/2022-01-10(週一).md 01. 個人/01. Daily/2022/01/2022-01-13(週四).md 01. 個人/01. Daily/2022/01/2022-01-19(週三).md 01. 個人/01. Daily/2022/01/2022-01-24(週一).md 01. 個人/01. Daily/2022/01/2022-01-26(週三).md 01. 個人/01. Daily/2022/02/2022-02-10(週四).md 01. 個人/01. Daily/2022/02/2022-02-11(週五).md 01. 個人/01. Daily/2022/02/2022-02-14(週一).md 01. 個人/01. Daily/2022/02/2022-02-15(週二).md 01. 個人/01. Daily/2022/03/2022-03-04(週五).md 01. 個人/01. Daily/2022/03/2022-03-28(週一).md 01. 個人/01. Daily/2022/04/2022-04-06(週三).md 01. 個人/01. Daily/2022/04/2022-04-08(週五).md 01. 個人/01. Daily/2022/04/2022-04-12(週二).md 01. 個人/01. Daily/2022/04/2022-04-14(週四).md 01. 個人/01. Daily/2022/年度目標.md 01. 個人/02. 專注Study/20150803 - Android/ADB 取得 APK 的 icon.md 01. 個人/02. 專注Study/20150803 - Android/ADB.md 01. 個人/02. 專注Study/20150803 - Android/AOSP.md 01. 個人/02. 專注Study/20150803 - Android/Ktor.md 01. 個人/02. 專注Study/20150803 - Android/Service.md 01. 個人/02. 專注Study/20150803 - Android/Tools.md 01. 個人/02. 專注Study/20150803 - Android/UI.md 01. 個人/02. 專注Study/20200207 - Study RxKotlin.md 01. 個人/02. 專注Study/20220601 - C++/C++17 1.md 01. 個人/02. 專注Study/20220601 - C++/C++17.md 01. 個人/02. 專注Study/20220601 - C++/Class template.md 01. 個人/02. 專注Study/20220601 - C++/Structured binding declaration.md 01. 個人/02. 專注Study/20220601 - C++/for_each.md 01. 個人/02. 專注Study/20220601 - C++/lambda.md 01. 個人/02. 專注Study/20220601 - C++/lvalue.md 01. 個人/02. 專注Study/20220601 - C++/move operator.md 01. 個人/02. 專注Study/20220601 - C++/rvalue.md 01. 個人/02. 專注Study/20220601 - C++/智慧指標.md 02. PARA/02. Area(領域)/00000000 - 投資.md 02. PARA/02. Area(領域)/001. Study Android Programming.md 02. PARA/02. Area(領域)/002. Study RxKotlin.md 02. PARA/02. Area(領域)/003. LBRY.md 02. PARA/02. Area(領域)/004. IPFS.md 02. PARA/02. Area(領域)/005. 投資.md 02. PARA/03. Resources(資源)/00. Informations/核芯達.md 02. PARA/03. Resources(資源)/WEB Programming.md 02. PARA/03. Resources(資源)/vim.md 02. PARA/04. Archives(歸檔)/01. Project(專案)/002. Bolide firmware verfication.md 02. PARA/04. Archives(歸檔)/01. Project(專案)/003. Meetup firmware update check.md 02. PARA/04. Archives(歸檔)/01. Project(專案)/004. Group firmware update check.md 02. PARA/04. Archives(歸檔)/03. Resources(資源)/Make Google Docs Look Like Dropbox Paper.md 02. PARA/04. Archives(歸檔)/03. Resources(資源)/PARA筆記.md 03. 資料收集/01. 架站/01. Nginx Layer4 Reverse Proxy.md 03. 資料收集/01. 架站/02. SWAG Reverse proxy.md 03. 資料收集/01. 架站/03. Trojan.md 03. 資料收集/01. 架站/04. Gitea.md 03. 資料收集/50. 軍武/虎式.md 03. 資料收集/51. 模型/Traxxas Sledge.md 03. 資料收集/51. 模型/舊化作例.md 03. 資料收集/802.11.md 03. 資料收集/99. templates/blogHeader.md 03. 資料收集/99. templates/date.md 03. 資料收集/99. templates/front matter.md 03. 資料收集/99. templates/note.md 03. 資料收集/99. templates/table.md 03. 資料收集/99. templates/thisWeek.md 03. 資料收集/99. templates/日記.md 03. 資料收集/99. templates/讀書筆記.md 03. 資料收集/Android operating.md 03. 資料收集/Android programming.md 03. 資料收集/COM/20210726 - COM Interface.md 03. 資料收集/Design Pattern.md 03. 資料收集/FFMPEG/00. Introduction.md 03. 資料收集/FFMPEG/01. Setup.md 03. 資料收集/FFMpeg.md 03. 資料收集/Flask.md 03. 資料收集/HDR Sensor.md 03. 資料收集/HTTP Server/Apache.md 03. 資料收集/HTTP Server/Nginx.md 03. 資料收集/IPFS.md 03. 資料收集/Linux/CLI.md 03. 資料收集/Linux/Programming.md 03. 資料收集/Linux/Timezone.md 03. 資料收集/Linux/Ubuntu.md 03. 資料收集/Linux/scp.md 03. 資料收集/Media Foundation/20210604 - Windows media foundation.md 03. 資料收集/MkDocs.md 03. 資料收集/MySQL.md 03. 資料收集/NextDNS.md 03. 資料收集/Nginx.md 03. 資料收集/Obsidian.md 03. 資料收集/OpenCV.md 03. 資料收集/OpenGL.md 03. 資料收集/Pelican blog.md 03. 資料收集/Proxmox VE.md 03. 資料收集/Python/argparse.ArgumentParser.md 03. 資料收集/Python/decorator.md 03. 資料收集/Python/logging.md 03. 資料收集/Python/opencv.md 03. 資料收集/Python/subprocess.md 03. 資料收集/Python/threading.md 03. 資料收集/Python/tkinter.md 03. 資料收集/Python/檢測工具.md 03. 資料收集/QT/Dropdown button.md 03. 資料收集/QT/QVariant.md 03. 資料收集/QT/Qt.md 03. 資料收集/Qt.md 03. 資料收集/RC.md 03. 資料收集/RaspberryPi.md 03. 資料收集/SLAM.md 03. 資料收集/Scripts.md 03. 資料收集/Storj.md 03. 資料收集/Tool Setup/Chrome.md 03. 資料收集/Tool Setup/Obisidian.md 03. 資料收集/Tool Setup/SublimeText.md 03. 資料收集/Tool Setup/VirtualBox.md 03. 資料收集/Tool Setup/Visual Studio Code.md 03. 資料收集/Tool Setup/Windows Setup.md 03. 資料收集/Tool Setup/Windows Terminal.md 03. 資料收集/Tool Setup/freefilesync.md 03. 資料收集/Tool Setup/vim.md 03. 資料收集/Trojan.md 03. 資料收集/UML.md 03. 資料收集/V2Ray.md 03. 資料收集/__其他.md 03. 資料收集/docker.md 03. 資料收集/frp.md 03. 資料收集/git/apply.md 03. 資料收集/git/submodule.md 03. 資料收集/sqlite.md 03. 資料收集/名言佳句.md 03. 資料收集/每週外食.md 03. 資料收集/演算法.md 03. 資料收集/翻牆/V2Ray.md 03. 資料收集/翻牆/Wireguard.md 03. 資料收集/讀書筆記/20201201 - 學習如何學習.md 03. 資料收集/讀書筆記/20201218 - Kotlin權威2.0.md 03. 資料收集/讀書筆記/20201224 - 寫作是最好的自我投資.md 03. 資料收集/讀書筆記/20210119 - 中產悲歌.md 03. 資料收集/讀書筆記/20210220 - 最高學習法.md 03. 資料收集/讀書筆記/20210320 - 最高學以致用法.md 03. 資料收集/讀書筆記/20210406 - 精準購買.md 03. 資料收集/讀書筆記/20210723 - 高手學習.md 03. 資料收集/讀書筆記/20220526 - 深入淺出設計模式.md 03. 資料收集/讀書筆記/20220619 - 精確的力量.md 03. 資料收集/財經.md 03. 資料收集/量子電腦.md 03. 資料收集/鋰電池.md
This commit is contained in:
39
01. 個人/02. 專注Study/20220601 - C++/C++17 1.md
Normal file
39
01. 個人/02. 專注Study/20220601 - C++/C++17 1.md
Normal file
@@ -0,0 +1,39 @@
|
||||
- 變數宣告的方式變了
|
||||
- Old: `int a = 3;`
|
||||
- New: `int a {3};`
|
||||
|
||||
- `if`裡面可以宣告變數
|
||||
```cpp
|
||||
if (auto a {3}; a > b) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
- `unique_ptr`: 無法複製的指標
|
||||
- 傳統方法:
|
||||
```cpp
|
||||
unique_ptr<uint8_t[]> buffer = new uint8_t[256];
|
||||
```
|
||||
- 新方法:
|
||||
```cpp
|
||||
auto buffer = std::make_unique<uint8_t[]>(256);
|
||||
```
|
||||
- `share_ptr`: 可以複製,但要避免循環參考問題
|
||||
|
||||
- 透過refernce傳遞array參數
|
||||
- 考慮一個帶有長度的陣列要傳到function裡面,但是又希望在function面可以指定陣列長度
|
||||
```cpp
|
||||
double value[] { 1.0, 2.0, 3.0 }; // Error!
|
||||
double value[] { 1.0, 2.0, 3.0, 4.0, 5.0 }; // Pass!
|
||||
|
||||
double average(const double (&array)[5]) {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
- 用 `std::string_view` 代替 `const std::string&`。
|
||||
|
||||
## Multi-Thread
|
||||
### 使用`std::async`
|
||||
- [C++ 使用 Async 非同步函數開發平行化計算程式教學](https://blog.gtwang.org/programming/cpp-11-async-function-parallel-computing-tutorial/)
|
||||
- [std::atomic](https://en.cppreference.com/w/cpp/atomic/atomic)
|
||||
39
01. 個人/02. 專注Study/20220601 - C++/C++17.md
Normal file
39
01. 個人/02. 專注Study/20220601 - C++/C++17.md
Normal file
@@ -0,0 +1,39 @@
|
||||
- 變數宣告的方式變了
|
||||
- Old: `int a = 3;`
|
||||
- New: `int a {3};`
|
||||
|
||||
- `if`裡面可以宣告變數
|
||||
```cpp
|
||||
if (auto a {3}; a > b) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
- `unique_ptr`: 無法複製的指標
|
||||
- 傳統方法:
|
||||
```cpp
|
||||
unique_ptr<uint8_t[]> buffer = new uint8_t[256];
|
||||
```
|
||||
- 新方法:
|
||||
```cpp
|
||||
auto buffer = std::make_unique<uint8_t[]>(256);
|
||||
```
|
||||
- `share_ptr`: 可以複製,但要避免循環參考問題
|
||||
|
||||
- 透過refernce傳遞array參數
|
||||
- 考慮一個帶有長度的陣列要傳到function裡面,但是又希望在function面可以指定陣列長度
|
||||
```cpp
|
||||
double value[] { 1.0, 2.0, 3.0 }; // Error!
|
||||
double value[] { 1.0, 2.0, 3.0, 4.0, 5.0 }; // Pass!
|
||||
|
||||
double average(const double (&array)[5]) {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
- 用 `std::string_view` 代替 `const std::string&`。
|
||||
|
||||
## Multi-Thread
|
||||
### 使用`std::async`
|
||||
- [C++ 使用 Async 非同步函數開發平行化計算程式教學](https://blog.gtwang.org/programming/cpp-11-async-function-parallel-computing-tutorial/)
|
||||
- [std::atomic](https://en.cppreference.com/w/cpp/atomic/atomic)
|
||||
23
01. 個人/02. 專注Study/20220601 - C++/Class template.md
Normal file
23
01. 個人/02. 專注Study/20220601 - C++/Class template.md
Normal file
@@ -0,0 +1,23 @@
|
||||
> Class template(類別樣板)不是類別,而是建立類別的方法。
|
||||
|
||||
定義類別樣板
|
||||
```cpp
|
||||
template <template parameter list>
|
||||
class ClassName
|
||||
{
|
||||
// Template class definition
|
||||
};
|
||||
```
|
||||
|
||||
用`typename`來指定會變動的變數型態,例:
|
||||
```cpp
|
||||
template <typename T1, typename T2>
|
||||
class MyTemplateClass
|
||||
{
|
||||
public:
|
||||
T1 length;
|
||||
T2 weight;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
Structured binding declaration 可以把對應的 tuple、pair、vector 展開,讓 code 更好讀。
|
||||
|
||||
## 展開 tuple
|
||||
假設我們有一個 tuple:
|
||||
```cpp
|
||||
std::tuple<std::string, uint32_t, uint32_t> person{ "John", 32, 170 };
|
||||
|
||||
auto& [name, age, tall] = person;
|
||||
```
|
||||
|
||||
name 會是 "John"
|
||||
age 是 32
|
||||
tall 是 170
|
||||
|
||||
但比較好用的時候還是用來展開 function 的回傳值,假設我們有一個 function 會回傳 tuple:
|
||||
```cpp
|
||||
std::tuple<std::string, uint32_t, uint32_t> getPersonData(uint32_t id) {
|
||||
return std::make_tuple("John", 32, 170);
|
||||
}
|
||||
|
||||
auto [name, age, tall] = getPersonData(id);
|
||||
```
|
||||
|
||||
## 展開 array
|
||||
`std::vector` 也是一樣的用法:
|
||||
```cpp
|
||||
#include <array>
|
||||
|
||||
std::array<int, 3> my_vec = { 5, 7, 10 };
|
||||
auto& [num1, num2, num3] = my_vec;
|
||||
```
|
||||
|
||||
或是:
|
||||
```cpp
|
||||
float rect[4]{ 5.0f, 6.0f, 120.0f, 200.0f };
|
||||
auto& [x, y, w, h] = rect;
|
||||
```
|
||||
|
||||
但是不能用來展開 `std::vector`。
|
||||
|
||||
## 展開 pair
|
||||
```cpp
|
||||
std::pair<std::string, int32_t> name_phone{ "John", 912345678 };
|
||||
auto& [name, phone_number] = name_phone;
|
||||
```
|
||||
|
||||
用在 for-loop 裡也比較好懂,假設我們有一個 vector 用來存剛剛的姓名跟電話:
|
||||
```cpp
|
||||
std::vector<std::pair<std::string, uint32_t>> phoneBook = {
|
||||
{ "John", 912345678 },
|
||||
{ "Andy", 912345679 },
|
||||
};
|
||||
|
||||
for (const auto& [name, phone] : phoneBook) {
|
||||
std::cout << "Name: " << name << ", phone: " << phone << std::endl;
|
||||
}
|
||||
```
|
||||
24
01. 個人/02. 專注Study/20220601 - C++/for_each.md
Normal file
24
01. 個人/02. 專注Study/20220601 - C++/for_each.md
Normal file
@@ -0,0 +1,24 @@
|
||||
for_each 是一個 function,它的原型是:
|
||||
```cpp
|
||||
template<class InputIterator, class Function>
|
||||
Function for_each(
|
||||
InputIterator _Start,
|
||||
InputIterator _Last,
|
||||
Function _Func
|
||||
);
|
||||
```
|
||||
|
||||
它需要3個參數,第1個是開始的iterator,第2是結束的 iterator,第3個是要用來處理的 function。
|
||||
|
||||
一個簡單的例子,有一個array,需要把每一個數都加1:
|
||||
```cpp
|
||||
vector<int> arr1 = { 4, 5, 8, 3, 1 };
|
||||
|
||||
for_each(
|
||||
arr1.begin(), // _Start
|
||||
arr1.end(), // _Last
|
||||
[](int& val) { // _Func
|
||||
val += 1;
|
||||
}
|
||||
);
|
||||
```
|
||||
223
01. 個人/02. 專注Study/20220601 - C++/lambda.md
Normal file
223
01. 個人/02. 專注Study/20220601 - C++/lambda.md
Normal file
@@ -0,0 +1,223 @@
|
||||
---
|
||||
tags:
|
||||
aliases:
|
||||
date: 2022-06-12
|
||||
time: 18:21:42
|
||||
description:
|
||||
---
|
||||
|
||||
一個簡單的 Lamdba 運算式:
|
||||
```cpp
|
||||
[] (int x, int y) -> bool {
|
||||
return x < y;
|
||||
}
|
||||
```
|
||||
|
||||
- 以中括號開頭,中括號被稱為*lamdba 導入器(lamdba introducer)*
|
||||
- 小括號裡面是*lamdba 參數列表(lambda parameter list)*
|
||||
- 如果沒有參數,小括號可以省略,`[] () {...}` 可以簡寫成 `[] {...}`
|
||||
- 箭號(`->`)後面是回傳的型別,如果沒寫就由 `return` 自動推斷
|
||||
|
||||
將 Lamdba 運算式指定給變數:
|
||||
```cpp
|
||||
auto comapre = [] (int x, int y) -> bool {
|
||||
return x < y;
|
||||
};
|
||||
```
|
||||
|
||||
## Lamdba的擷取子句
|
||||
以中括號開頭的 *lamdba 導入器* 可以將外部的變數傳給 Lamdba 運算式,正式名稱是「擷取子句(capture clause)」。
|
||||
`[=]` 表示它們會以值擷取(captured by value)。Scope內的變數可以在 lamdba 內使用,但是不可以改變。
|
||||
`[&]` 表示它們會以參考擷取(captured by reference)。Scope內的變數可以在 lamdba 內使用,可以改變。
|
||||
|
||||
## 以值擷取(captured by value)
|
||||
假設有一段程式如下:
|
||||
```cpp
|
||||
void testLambda() {
|
||||
float notUsed = 1.0f;
|
||||
std::vector<int32_t> numlist{10, 20, 30, 50, 60};
|
||||
auto findInRange = [=](int32_t start, int32_t end) {
|
||||
for (auto num : numlist) {
|
||||
if (num >= start && num <= end) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
std::cout << "Result: " << findInRange(25, 35) << "\n";
|
||||
}
|
||||
```
|
||||
|
||||
用`[=]`可以用來擷取 lamdba scope 範圍所及的變數,沒有在 Lamdba 運算式裡面被用到的變數就不會被擷取,例如 `float notUsed = 1.0f;`。
|
||||
另一個重點是:**被擷取的變數是不可以更改的**。例如,不能在 lambda 裡面這樣寫:
|
||||
```cpp
|
||||
auto findInRange = [=](int32_t start, int32_t end) {
|
||||
numlist.push_back(5); // ERROR!
|
||||
|
||||
for (auto num : numlist) {
|
||||
if (num >= start && num <= end) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
```
|
||||
|
||||
如果一定要在 lambda 內改變擷取的變數,那必須指名 lambda 為 `mutable`:
|
||||
```cpp
|
||||
auto findInRange = [=](int32_t start, int32_t end) mutable { // <-- assign mutable
|
||||
numlist.push_back(5);
|
||||
|
||||
for (auto num : numlist) {
|
||||
if (num >= start && num <= end) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
```
|
||||
|
||||
根據書上解釋 ,可以裡解為 compiler 會將 lamdba 編為一個 class,像是:
|
||||
```cpp
|
||||
class __Lambda8C1A5 {
|
||||
public:
|
||||
__Lambda8C1A5(const std::vector<int32_t>& arg1) : numlist(arg1) {}
|
||||
auto operator()(int32_t start, int32_t end) const { // const!
|
||||
for (auto num : numlist) {
|
||||
if (num >= start && num <= end) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<int32_t> numlist;
|
||||
};
|
||||
```
|
||||
|
||||
這也解釋了 lamdba 的擷取範圍與原理。而 `mutable` 則是讓 `operator()` 不為 `const`,如下:
|
||||
```cpp
|
||||
auto findInRange = [=](int32_t start, int32_t end) mutable { // <-- assign mutable
|
||||
numlist.push_back(5);
|
||||
|
||||
for (auto num : numlist) {
|
||||
if (num >= start && num <= end) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
...
|
||||
|
||||
class __Lambda8C1A5 {
|
||||
public:
|
||||
__Lambda8C1A5(const std::vector<int32_t>& arg1) : numlist(arg1) {}
|
||||
auto operator()(int32_t start, int32_t end) { // No const here
|
||||
for (auto num : numlist) {
|
||||
if (num >= start && num <= end) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<int32_t> numlist;
|
||||
};
|
||||
```
|
||||
|
||||
## 以值擷取特定的變數
|
||||
若只需要擷取特定的變數,那就直接在 lamdba 導入器(就是`[]`)寫入變數名稱,例如:
|
||||
```cpp
|
||||
int var1 = 10;
|
||||
int var2 = 20;
|
||||
int var3 = 30;
|
||||
|
||||
auto afunc = [var1, var2] () {
|
||||
...
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
## 以參考擷取(captured by reference)
|
||||
`[&]` 會擷取 scope 內的所有外部變數,而且可以修改:
|
||||
```cpp
|
||||
void testLambda() {
|
||||
float notUsed = 1.0f;
|
||||
std::vector<int32_t> numlist{ 10, 20, 30, 50, 60 };
|
||||
auto findInRange = [&](int32_t start, int32_t end) { // Use & here
|
||||
numlist.push_back(100); // OK
|
||||
|
||||
for (auto num : numlist) {
|
||||
if (num >= start && num <= end) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
std::cout << "Result: " << findInRange(25, 35) << "\n";
|
||||
std::cout << "numlist: ";
|
||||
for (auto n : numlist) {
|
||||
std::cout << n << " ";
|
||||
}
|
||||
std::cout << "\n"; // Output numlist: 10 20 30 50 60 100
|
||||
}
|
||||
```
|
||||
|
||||
## 以參考擷取特定的變數
|
||||
但是直接參考全部的外部變數不是好的作法,這讓你有機會做出一些意外的修改,所以請擷取有需要的變數就好:
|
||||
```cpp
|
||||
void testLambda() {
|
||||
float notUsed = 1.0f;
|
||||
std::vector<int32_t> numlist{ 10, 20, 30, 50, 60 };
|
||||
|
||||
auto findInRange = [&numlist](int32_t start, int32_t end) {
|
||||
numlist.push_back(100); // OK
|
||||
|
||||
for (auto num : numlist) {
|
||||
if (num >= start && num <= end) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
如果有多個變數需要擷取,那就用 `,` 分開:
|
||||
```cpp
|
||||
auto findInRange = [&numlist, &var1, &var2](int32_t start, int32_t end) {
|
||||
...
|
||||
};
|
||||
```
|
||||
|
||||
## 混合擷取
|
||||
以值擷取跟參考擷取也可以寫在一起:
|
||||
```cpp
|
||||
auto findInRange = [=, &numlist](int32_t start, int32_t end) {
|
||||
...
|
||||
};
|
||||
```
|
||||
上面的例子中,`numlist` 會是參考擷取,其他的外部變數則是以值擷取。
|
||||
|
||||
或是:
|
||||
```cpp
|
||||
auto findInRange = [&, numlist](int32_t start, int32_t end) {
|
||||
...
|
||||
};
|
||||
```
|
||||
上面的例子中,`numlist` 會以值擷取,其他的外部變數則是參考擷取。
|
||||
|
||||
但是,如果已經使用了 `=` ,就不可以再以值擷取其他變數,像是 `[=, numlist]` 就是不合法的。
|
||||
反之,如果已經使用了 `&`,就不可以再參考擷取其他變數,像是 `[&, &var1]` 就是不合法的。
|
||||
|
||||
|
||||
## 存取 class
|
||||
Lamdba 寫在 class 裡面的時候,不論 [[lambda#以值擷取(captured by value)|以值擷取]]或是 [[lambda#以參考擷取(captured by reference)|以參考擷取]]都沒辦法傳遞成員變數(member variable),只能傳遞 `this`,透過 `this` 來存取成員變數。例:
|
||||
```cpp
|
||||
class BigBuffer {
|
||||
public:
|
||||
void modify(int x, int y, ...) {
|
||||
auto modifyBuffer = [this] () { // Use this
|
||||
if (buffer) { // equal to this->buffer
|
||||
// do something with buffer
|
||||
}
|
||||
};
|
||||
...
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t bufferSize = 0;
|
||||
std::unique_ptr<uint8_t[]> buffer = nullptr;
|
||||
};
|
||||
```
|
||||
3
01. 個人/02. 專注Study/20220601 - C++/lvalue.md
Normal file
3
01. 個人/02. 專注Study/20220601 - C++/lvalue.md
Normal file
@@ -0,0 +1,3 @@
|
||||
lvalue 是指:
|
||||
- 等號左邊的值
|
||||
- 可以被「取址」的變數
|
||||
59
01. 個人/02. 專注Study/20220601 - C++/move operator.md
Normal file
59
01. 個人/02. 專注Study/20220601 - C++/move operator.md
Normal file
@@ -0,0 +1,59 @@
|
||||
move operator可以讓[[rvalue]]被參考,從而進一部的消除複製的成本。例如,以下的function會回傳一個很大的陣列:
|
||||
```cpp
|
||||
vector<int> generateBigArray() {
|
||||
const int size = 1000000;
|
||||
vector<int> array;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
array[i] = i;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
||||
當我們呼叫這個function並把結果回傳到一個變數的時候,每一次這個大陣列都會被複製一次,例:
|
||||
```cpp
|
||||
vector<int> atemp = generateBigArray(); // 複製1000000個int
|
||||
```
|
||||
|
||||
如果使用[[rvalue]] reference就可以避開這些複製,例:
|
||||
```cpp
|
||||
vector<int>&& atemp = generateBigArray(); // 已經建立好的array會直接「移動」到atemp,省下了複製的步驟
|
||||
```
|
||||
|
||||
## move contructor
|
||||
move contructor跟copy constructor很類似,只是參數由`&`改成了`&&`。
|
||||
例:
|
||||
```cpp
|
||||
template <typename T>
|
||||
inline Array<T>::Array(const Array&& moved) :
|
||||
size{moved.size},
|
||||
elements{moved.elements}
|
||||
{
|
||||
moved.elements = nullptr;
|
||||
}
|
||||
```
|
||||
|
||||
## move assignment operator
|
||||
```cpp
|
||||
template <typename T>
|
||||
Array<T>& Array<T>::operator=(const Array&& rhs)
|
||||
{
|
||||
if (this != &rhs) {
|
||||
delete [] elements;
|
||||
elements = rhs.elements;
|
||||
size = rhs.size;
|
||||
rhs.elements = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
```
|
||||
|
||||
## 明確的「移動」
|
||||
如果有一個現存的「大東西」,可以使用`std::move`來把它「移」到別的地方,進而避開了複製的行為。例:
|
||||
```cpp
|
||||
std::vector<std::string> my_dictionary(10000000);
|
||||
std::vector<std::string> dictionary2 = std::move(my_dictionary);
|
||||
```
|
||||
在`std::move`之後,my_dictionary的size會變成0。
|
||||
210
01. 個人/02. 專注Study/20220601 - C++/rvalue.md
Normal file
210
01. 個人/02. 專注Study/20220601 - C++/rvalue.md
Normal file
@@ -0,0 +1,210 @@
|
||||
rvalue 是指:
|
||||
- 等號右邊的值
|
||||
- 臨時的值,例如運算的結果
|
||||
- 無法被取址(address-of)的物件
|
||||
|
||||
## rvalue reference
|
||||
一般的參考只能參考[[lvalue]],如下的程式是ok的:
|
||||
```cpp
|
||||
int a = 10;
|
||||
int& b = a;
|
||||
```
|
||||
|
||||
但是像這樣就不行了:
|
||||
```cpp
|
||||
int a = 10;
|
||||
int b = 5;
|
||||
int& c = a + b;
|
||||
```
|
||||
|
||||
因為`a+b`是一個 rvalue(臨時的值,沒辦法取址),所以無法參考。
|
||||
但是可以用`&&`來參考 rvalue。例如:
|
||||
```cpp
|
||||
int a = 10;
|
||||
int b = 5;
|
||||
int&& c = a + b; // c = 15
|
||||
```
|
||||
|
||||
而不用這樣:
|
||||
```cpp
|
||||
int a = 10;
|
||||
int b = 5;
|
||||
int r = a + b;
|
||||
int& c = r;
|
||||
```
|
||||
|
||||
了解 rvalue reference 之後,就可以實作類別的 move constructor 跟 move assignment operator。這可以減少複製的成本。
|
||||
|
||||
## Move constructor
|
||||
假設我們有一個 class 叫 BigBuffer,定義如下:
|
||||
```cpp
|
||||
class BigBuffer {
|
||||
public:
|
||||
BigBuffer(int size=100*1024*1024) :
|
||||
bufferSize(size)
|
||||
{
|
||||
std::cout << "BigBuffer constructor\n";
|
||||
this->buffer = std::make_unique<uint8_t[]>(bufferSize);
|
||||
}
|
||||
|
||||
~BigBuffer() {
|
||||
std::cout << "BigBuffer destructor\n";
|
||||
}
|
||||
|
||||
BigBuffer(const BigBuffer& src) {
|
||||
std::cout << "BigBuffer copy constructor\n";
|
||||
bufferSize = src.bufferSize;
|
||||
buffer = std::make_unique<uint8_t[]>(bufferSize);
|
||||
std::memcpy(buffer.get(), src.buffer.get(), bufferSize);
|
||||
}
|
||||
|
||||
BigBuffer& operator= (BigBuffer& src) {
|
||||
std::cout << "BigBuffer copy operator\n";
|
||||
bufferSize = src.bufferSize;
|
||||
buffer = std::make_unique<uint8_t[]>(bufferSize);
|
||||
std::memcpy(buffer.get(), src.buffer.get(), bufferSize);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
int bufferSize = 0;
|
||||
std::unique_ptr<uint8_t[]> buffer = nullptr;
|
||||
};
|
||||
```
|
||||
|
||||
這個 class 的特色就是每一次使用都會佔用100MB的記憶體空間,想像下面的程式的動作:
|
||||
```cpp
|
||||
BigBuffer buf1;
|
||||
// Do something with buf1
|
||||
// Assign to buf2
|
||||
BigBuffer buf2 = buf1;
|
||||
```
|
||||
|
||||
執行訊息:
|
||||
```
|
||||
BigBuffer constructor // create buf1
|
||||
BigBuffer copy constructor, copy 104857600Bytes // copy buf1 to buf2
|
||||
...
|
||||
```
|
||||
|
||||
這會先產生 buf1,然後把 buf1 copy 給 buf2。如果我們想要省下 copy 的成本,這時候 Move constructor 就可以派上用場了。
|
||||
幫 BigBuffer 加一個 Move constructor:
|
||||
```cpp
|
||||
class BigBuffer {
|
||||
public:
|
||||
...
|
||||
|
||||
BigBuffer(BigBuffer&& src) noexcept {
|
||||
std::cout << "BigBuffer move constructor\n";
|
||||
bufferSize = src.bufferSize;
|
||||
buffer = std::move(src.buffer);
|
||||
|
||||
src.buffer.reset();
|
||||
src.bufferSize = 0;
|
||||
}
|
||||
...
|
||||
};
|
||||
```
|
||||
|
||||
這個 move constructor 的參數就是一個 rvalue reference,我們把來源的 bufferSize 跟 buffer 指標「移到」我們這邊,而不是完整的複製一份。在轉移之後呢,當然也要把來源清空,讓轉移更加明確。
|
||||
|
||||
有了 Move assignment operator 之後,在執行一次原本的程式,你會發現訊息......沒有變,還是一樣呼叫 copy constructor 來複製了100MB 的 buffer,這時我們需要明確的告訴 compiler 我們要「移動」物件,而不是複製它,把原本的程式改為:
|
||||
```cpp
|
||||
BigBuffer buf1;
|
||||
// Do something with buf1
|
||||
// Assign to buf2
|
||||
BigBuffer buf2 = std::move(buf1);
|
||||
```
|
||||
|
||||
我們用 `std::move()` 來「移動」物件,這時輸出變成
|
||||
```
|
||||
BigBuffer constructor // create buf1
|
||||
BigBuffer move constructor // move buf1 to buf2, buf1 has nullptr now
|
||||
...
|
||||
```
|
||||
|
||||
另外一個情形也可以受益於此,假如我們有個 function 會產生 `BigBuffer`,如下:
|
||||
```cpp
|
||||
BigBuffer BigBufferCreator() {
|
||||
std::cout << "BigBufferCreator: Create a BigBuffer!\n";
|
||||
BigBuffer tempb;
|
||||
// do something
|
||||
std::cout << "BigBufferCreator: return\n";
|
||||
return tempb;
|
||||
}
|
||||
|
||||
BigBuffer b = BigBufferCreator(); // copy tempb to b
|
||||
```
|
||||
|
||||
在沒有 Move constructor 的情況下,上面的程式會先產生一個 `tempb`,然後複製給 `b`,訊息:
|
||||
```
|
||||
BigBufferCreator: Create a BigBuffer!
|
||||
BigBuffer constructor
|
||||
BigBufferCreator: return
|
||||
BigBuffer copy constructor, copy 104857600Bytes // Copy 100MB!
|
||||
...
|
||||
```
|
||||
|
||||
在有 Move constructor 的情況下,訊息就變成:
|
||||
```
|
||||
BigBufferCreator: Create a BigBuffer!
|
||||
BigBuffer constructor
|
||||
BigBufferCreator: return
|
||||
BigBuffer move constructor // Use MOVE!
|
||||
BigBuffer destructor
|
||||
BigBuffer destructor
|
||||
```
|
||||
|
||||
因為 `BigBufferCreator()` 產生的就是一個 `BigBuffer` rvalue,所以 compiler 會使用 move constructor(`BigBuffer(BigBuffer&& src)`) 而不是 copy constructor。
|
||||
|
||||
## Move assignment operator(`=`)
|
||||
Move assignment operator 的行為跟 move constructor 是一樣的,幫 `BigBuffer` 加入 move assignment operator:
|
||||
```cpp
|
||||
class BigBuffer {
|
||||
public:
|
||||
...
|
||||
|
||||
BigBuffer& operator=(BigBuffer&& src) noexcept {
|
||||
std::cout << "BigBuffer move operator\n";
|
||||
bufferSize = src.bufferSize;
|
||||
buffer = std::move(src.buffer);
|
||||
|
||||
src.buffer.reset();
|
||||
src.bufferSize = 0;
|
||||
return *this;
|
||||
}
|
||||
...
|
||||
};
|
||||
```
|
||||
|
||||
測試程式:
|
||||
```cpp
|
||||
BigBuffer b1, b2;
|
||||
b2 = b1;
|
||||
```
|
||||
|
||||
訊息:
|
||||
```
|
||||
BigBuffer constructor
|
||||
BigBuffer constructor
|
||||
BigBuffer copy operator, copy 104857600Bytes
|
||||
```
|
||||
|
||||
還是使用 copy assignment operator 來複製,理由是一樣的,需要一個明確的 `std::move()` 來表示「轉移」的行動,把程式改成:
|
||||
```cpp
|
||||
BigBuffer b1, b2;
|
||||
b2 = std::move(b1);
|
||||
```
|
||||
|
||||
這樣就可以了。訊息:
|
||||
```
|
||||
BigBuffer constructor
|
||||
BigBuffer constructor
|
||||
BigBuffer move operator // Use MOVE!
|
||||
```
|
||||
|
||||
## 參考
|
||||
- [Value categories - cppreference.com](https://en.cppreference.com/w/cpp/language/value_category)
|
||||
- [rvalue 參考](https://openhome.cc/Gossip/CppGossip/RvalueReference.html)
|
||||
- [Move constructors - cppreference.com](https://en.cppreference.com/w/cpp/language/move_constructor)
|
||||
- [Move assignment operator - cppreference.com](https://en.cppreference.com/w/cpp/language/move_assignment)
|
||||
110
01. 個人/02. 專注Study/20220601 - C++/智慧指標.md
Normal file
110
01. 個人/02. 專注Study/20220601 - C++/智慧指標.md
Normal file
@@ -0,0 +1,110 @@
|
||||
[`unique_ptr`](https://en.cppreference.com/w/cpp/memory/unique_ptr)與[`shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr)都是智慧指標,箱對於原本的raw pointer,智慧指標使用起來更方便,也不用擔心delete的問題。
|
||||
|
||||
## unique_ptr
|
||||
`unique_ptr` 的特點是,它保證在一個時間內,只會有一個指標的擁有者,也就是這個指標不能被複製跟移動,當 `unique_ptr` 離開它的scope時候,它所擁有的指標也隨之被delete。這讓你不用擔心memory leak的問題。
|
||||
假設我們有一個class叫 `BigBuffer` ,原本分配記憶體的方法:
|
||||
```cpp
|
||||
BigBuffer* bigBuf = new BigBuffer(bufferSize);
|
||||
// Use buffer here
|
||||
delete bigBuf;
|
||||
```
|
||||
|
||||
用 `unique_ptr`:
|
||||
```cpp
|
||||
auto bigBuf = std::make_unique<BigBuffer>(bufferSize);
|
||||
// Use buffer here
|
||||
// bigBuf will be released when exiting scope
|
||||
```
|
||||
|
||||
我們統一用[`std::make_unique<>`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique)這個template function來建立 `unique_ptr` ,角括號 `<>` 裡面要帶入你要建立的型別,後面的括號 `()` 就是型別的constructor,使用起來跟 `new` 是一樣的。
|
||||
因為 `std::make_unique<>` 裡面已經有表明型別了,所以變數就用 `auto` 就可以了,不用再寫一次型別。
|
||||
|
||||
一旦 `unique_ptr` 建立之後,使用起來就跟一般指標沒有兩樣,都是用 `->` 來操作:`bigBuf->setXXX()` or `bigBuf->getXXX()`。
|
||||
但是別忘記 `unique_ptr` 本身還是一個local variable,所以我們可以用 `.` 來操作 `unique_ptr` ,例如我們可以用 `.reset()` 重新配一個指標:
|
||||
```cpp
|
||||
BigBuffer* pBuffer = new BigBuffer();
|
||||
bigBuf.reset(pBuffer);
|
||||
```
|
||||
這時候舊指標會自動delete,如果記憶體分配有成功的話,bigBuf會接管剛剛new出來的指標,或者變成 `nullptr` (記憶體分配失敗)。
|
||||
|
||||
如果單純想要釋放指標,那就單純的呼叫 `reset()` 就好。
|
||||
```cpp
|
||||
bigBuf.reset(); // Now I'm nullptr
|
||||
```
|
||||
|
||||
如果要分配陣列的話:
|
||||
```cpp
|
||||
auto intArray = std::make_unique<int[]>(1024);
|
||||
```
|
||||
|
||||
使用方式也是一樣的:
|
||||
```cpp
|
||||
intArray[5] = 555;
|
||||
```
|
||||
|
||||
不過對於陣列的操作更建議使用 `std::array` 。
|
||||
|
||||
如果有什麼特殊原因讓你決定不再讓 `unique_ptr` 來幫你管理指標,可以用 `release()` 來讓出指標:
|
||||
```cpp
|
||||
auto intArray = std::make_unique<int[]>(1024);
|
||||
int* intArrayRaw = intArray.release(); // Now I don't care anymore
|
||||
```
|
||||
但是這時候呼叫 `delete[]` (或 `delete` )的責任又回到你身上了。所以千萬不要把 `release()` 跟 `reset()` 搞混了。
|
||||
|
||||
`unique_ptr` 不能被複製跟移動,所以下列的寫法都編不過:
|
||||
```cpp
|
||||
auto ptr1 = std::make_unique<int>(5);
|
||||
std::unique_ptr<int> ptr2(ptr1); // Error
|
||||
std::unique_ptr<int> ptr2 = ptr1; // Error
|
||||
```
|
||||
在Visual Studio 2017上,錯誤訊息是這樣:`error C2280: 'std::unique_ptr<int,std::default_delete<int>>::unique_ptr(const std::unique_ptr<int,std::default_delete<int>> &)': attempting to reference a deleted function`。
|
||||
其實就是`unique_ptr`的copy constructor跟assignment operator都被標記為delete了。
|
||||
|
||||
### Move a unique_ptr
|
||||
如果一定要把 unique_ptr 指定給別人可以嗎?可以的,用 `std::move()` 來轉移:
|
||||
```cpp
|
||||
auto ptr1 = std::make_unique<int>(5);
|
||||
// do something
|
||||
auto anotherPtr = std::move(ptr1);
|
||||
```
|
||||
|
||||
ptr1原本所管理的指標會轉移給 anotherPtr,ptr1 會變成 nullptr。
|
||||
|
||||
## shared_ptr
|
||||
建立一個 `shared_ptr` 是使用[`std::make_shared()`](https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared):
|
||||
```cpp
|
||||
auto myBuf = std::make_shared<BigBuffer>(bufferSize);
|
||||
```
|
||||
|
||||
但是 `shared_ptr` 可以被複製與移動,這是跟 `unique_ptr` 的差別:
|
||||
```cpp
|
||||
auto myBuf = std::make_shared<BigBuffer>(bufferSize);
|
||||
|
||||
std::shared_ptr<BigBuffer> bufCopy = myBuf;
|
||||
```
|
||||
|
||||
現在 bufCopy 跟 myBuf 都指向同一個指標,他們都可以操作這個指標:
|
||||
```cpp
|
||||
myBuf->setZero(startAddr, endAddr);
|
||||
bufCopy->setOne(startAddr, endAddr);
|
||||
```
|
||||
|
||||
`shared_ptr` 內部有一個參考記數(reference count)來紀錄它所擁有的指標已經分享給幾個變數了,只要有變數離開了他的scope,參考記數就會減少,反之,要是像上面那樣有人複製指標,參考記數就會增加,參考記數歸0的時候,指標就會被釋放。
|
||||
|
||||
有了 `shared_ptr` 我們就不必擔心 delete 的責任問題:
|
||||
```cpp
|
||||
std::shared_ptr<BigBuffer> getBuffer(int32_t bufferSize) {
|
||||
return std::make_shared<BigBuffer>(bufferSize);
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto myBuf = getBuffer(1024); // new(malloc) memory
|
||||
// use myBuf
|
||||
|
||||
return 0;
|
||||
} // myBuf delete memory here
|
||||
```
|
||||
|
||||
`shared_ptr` 有一個問題是可以會「循環參考」(cyclic references),也就是 share_ptr A 指向另一個 share_ptr B ,然後 share_ptr B 又指向 share_ptr A,這造成參考記數(reference count)不會減少而永遠無法釋出指標。這個是需要注意的。
|
||||
|
||||
但是 `shared_ptr` 還是讓記憶體的管理問題大大減少,應該用 `shared_ptr` 來代替 `new` & `delete` 。
|
||||
Reference in New Issue
Block a user