C++ Attributes In code, you might occasionally see [[nodiscard]] and other similar syntax in interesting places. These are called attributes, and they serve a specific purpose. Attributes in C++ ...

Item 30 - likely and unlikely Attributes(English)

Item 22 - reduce(中文)
std::reduce std::reduce 是 C++17 引入的標準函數,用於對範圍內的元素進行歸約操作。 它類似於 std::accumulate,但有一些重要的區別,包括默認使用並行執行和一些優化,真是太方便了! 累加範圍內的所有元素。 #include <numeric> #include <vector> #include <iostrea...

Item 22 - reduce(English)
std::reduce std::reduce is a standard function introduced in C++17 for performing reduction operations on a range of elements. It is similar to std::accumulate, but with some important difference...

Item 17 - shared_ptr Supports for Creating Dynamic Arrays (English)
Support for Creating Dynamic Arrays In C++11, std::shared_ptr does not support creating dynamic arrays. If you forgot, you can check this article. C++17 introduced support for this, making the fu...

Item 17 - shared_ptr 支援創建動態數組 (中文)
支援創建動態數組 C++11 時,std::shared_ptr 尚無支援創建動態數組,忘記的話可以看這篇 C++17 支援了,讓 std::shared_ptr 的功能變得更全面。 code #include <iostream> #include <memory> class Resource { public: Resource() { s...

Item 5(2/2) - Lambda expression (English)
捕捉變數 Let’s review the content from the previous articlebefore diving deeper. The basic syntax of a Lambda expression is as follows: [capture](parameters) -> return_type { body } If I have t...

Item 5(2/2) - Lambda expression 匿名函數(中文)
捕捉變數 回顧一下上一篇的內容,在往下討論。 Lambda 表達式的基本語法如下 [capture](parameters) -> return_type { body } 如果我要 capture 的變數超級多,那我要一一指名,lambda function 就會被寫得很醜, 有沒有比較優雅的方式去完成 ? Yes!! 捕獲模式 [=] 的含義 [=] 表示 按值捕獲...

Item 7 (2/2) - Smart pointers(English)
std::shared_ptr Circular Reference Issue Is std::shared_ptr smart enough for us to use it without worries? No!!! Although std::shared_ptr is indeed very useful, careless use can still lead to prob...

Item 7 (2/2)- 智能指针(smart pointers)(中文)
std::shared_ptr 循環引用(circular reference)問題 std::shared_ptr 很聰明,我們可以放心的使用它了嗎? No!!! 雖然 std::shared_ptr 真的很不錯,但是不小心使用還是會有問題! std::shared_ptr 的循環引用(circular reference)問題,是指當兩個或多個 std::shared_ptr 對象相...

Item 7 (1/2) - Smart pointers(English)
Smart Pointers When you see an int* ptr being used in your C++ code, do you need to delete it after use? If yes, should you use delete[] ptr or delete ptr? If the later code performs a delete, rep...

Item 7 (1/2) - 智能指针(smart pointers)(中文)
智能指针 當你看到一個 int* ptr 在你的 c++ code 中使用,用完後要不要去 delete 它 ? 如果要,請問是使用 delete [] ptr or delete ptr ? 若後面的 code 有做 delete , 則 重複 delete 會造成 crush. 如果不要,而後面沒有 code 做 delete ,則造成 memory leak。 上述的困境,你一定遇...

Item 27 - 模塊(modules)(中文)
模塊(modules) C++20 引入了模塊(modules),這是自 C++11 引入的範本(templates)以來最大的語言特性改變之一。 模塊旨在替代傳統的頭文件機制,以改善編譯時間並提供更好的封裝性。 傳統的 c++ include 真的好難用喔,像 python 都可直接 import 模組使用 ex: import numpy as np 我們來看一下 include 造...

Item 27 - Modules(English)
Modules C++20 introduced modules, which is one of the biggest language feature changes since the introduction of templates in C++11. Modules aim to replace the traditional header file mechanism to...

Item 23 - 概念(Concepts)(中文)
概念(Concepts) 在 C++20 中,概念(Concepts)是一種新的語法工具,用於約束模板參數,以提高模板代碼的可讀性和錯誤提示。 概念可以幫助開發者在編譯時期捕捉錯誤,並提供更具體的錯誤信息。 概念是用來定義模板參數所需滿足的條件。 這些條件通常是型別特性、方法簽名或其他編譯時期可檢查的性質。 概念使得模板代碼更加直觀和易於理解,並且可以大大減少模版使用錯誤時的錯誤訊息。 ...

Item 23 - Concepts(English)
Concepts In C++20, concepts are a new syntactical tool used to constrain template parameters, enhancing the readability of template code and improving error messages. Concepts help developers catc...

Item 28 - 指定初始化器 (中文)
指定初始化器 在 C++20 中,designated initializer 是一種語法,允許我們在初始化結構體或聯合體時,顯式地指定每個成員的初始化方式。這種語法源自 C 語言,在 C++20 中得到了支持。使用 designated initializer,可以在初始化列表中指定要初始化的特定成員,而不是按照成員在結構體或聯合體中定義的順序依次初始化。 基本語法: StructT...

Item 28 - Designated Initializer (English)
Designated Initializer In C++20, the designated initializer is a syntax that allows us to explicitly specify how each member of a structure or union is initialized when using an initializer list. ...

Item 25 - 協程(coroutines)(中文)
什麼是協程 相信很多人看到 c++20 提供的新功能協程,因該滿頭問號,更不用說去使用了? 到底協程是不是多線程? 直接說答案:不是! 協程本身並不是一種多線程技術,而是一種在單線程內實現非阻塞異步操作的方法。 協程允許在一個線程內進行高效的任務切換,而不需要多線程帶來的上下文切換和同步開銷。 了解到這點後,再去看 code 就會清楚很多了。 協程與多線程的區別 協程(Corouti...

Item 25 - Coroutines(English)
What are Coroutines? When many people see the new feature coroutines introduced in C++20, they might be puzzled and unsure how to use it. Are coroutines the same as multithreading? The answer is...

DownGit 下載 Github repo 的部分檔案 (中文)
下載 Github repo 的部分檔案 可以只下載 Github repo 的部分檔案嗎? Yes! 在 GitHub 上,你可以只下載某個公共倉庫的部分資料夾或檔案, 但需要一些技巧,因為 GitHub 的界面本身不支援直接下載部分內容。 其中最簡單的方式就是線上 DownGit 啦。link 打開 DownGit. 輸入你想要下載的資料夾的 URL,例如:https...

Download Specific Files from a GitHub Repository (English)
Download Specific Files from a GitHub Repository Is it possible to download only specific files from a GitHub repository? Yes! On GitHub, you can download only certain folders or files from a pu...

.h vs .hpp (中文)
.h vs .hpp 在 project 中,常常會看到 .h 和 .hpp 的檔案。 這個到底有什麼不同呢? .hpp 和 .h 文件在本質上是相同的 .hpp 和 .h 文件在本質上是相同的,都是C++和C語言中用於定義函數原型、類聲明、常量和宏定義的頭文件。它們的主要區別在於擴展名的命名慣例和用途的歷史來源。 .h 文件 擴展名:.h 是歷史上用於C語言頭文件的標準擴展名,...

.h vs .hpp (English)
.h vs .hpp In a project, you often see files with .h and .hpp extensions. What is the difference between them? .hpp and .h files are essentially the same .hpp and .h files serve the same funda...

Item 8 (2/2)- Enhanced support for multithreading(English)
Inter-thread Communication std::condition_variable is used for communication between threads, allowing one thread to wait for a notification from another thread. Multithreading #include <iost...

Item 8 (2/2)- 多線程支持的增強(中文)
線程間的通信 std::condition_variable 用於線程間的通信,允許一個線程等待另一個線程發送的通知。 多執行序 #include <iostream> #include <thread> #include <mutex> #include <condition_variable> std::mutex mtx; std...

Item 8(1/2) - 多線程支持的增強(中文)
多執行序 在 C++11 之前,C++ 並沒有標準化的多線程支持,因此需要依賴於平台特定的庫來實現多線程。 簡單來說,就是很麻煩。無法達到跨平台的效果, 例如: 在 Windows 上可以使用 WinAPI, 必須包含 <windows.h> 標頭檔,不需要額外安裝庫但僅適用於 Windows 系統。 而在 POSIX 系統(如 Linux 和 macOS)上則可以使用 ...

Item 8(1/2) - Enhanced Multithreading Support(English)
Multithreading in C++ Before C++11, C++ did not have standardized support for multithreading, which meant developers had to rely on platform-specific libraries to implement multithreading. This ap...

Item 29 - 三向比較運算符 <=> (中文)
三向比較運算符 當我們在寫自己的 struct or class 時,有時候要去該物件的比較, 也就是 <, <=, ==, !=, >=, > 這些 operator。 但寫過的人就知道,上述 6 個 operator 都要去寫非常麻煩。 而且還很容易錯誤。 C++20 引入了三向比較運算符(spaceship operator, <=>),它是一個...

Item 29 - Three-way Comparison Operator <=> (English)
Three-way Comparison Operator When writing your own struct or class, sometimes you need to compare objects, which involves implementing operators like <, <=, ==, !=, >=, and >. Impleme...

c++ 委派 delegate (中文)
Delegate 在 C++ 中實現 delegate(委派)功能,通常可以使用函數對象、std::function 以及 std::bind 來達成。這可以讓你把對某個函數的調用委派給另一個對象或函數。 以下是一個簡單的範例,展示如何使用這些工具來實現類似 delegate 的行為。 使用函數指針 #include <iostream> // 函數指針類型 typed...