前情提要
長久以來寫C/C++的Code時,配置環境是很麻煩的一件事,通常會遇到以下的事情
- 下載第三方Library
- 設定Include Path
- 設定Linking library
- Cross Platform的設置
- Makefile的編寫
雖然有了CMake幫助解決塊平台的問題。不過還是不夠。有時我們想要Rust的Cargo或是Go Build這樣的東西,而Biicode給了我們一點光明。
這篇是How to get started with the LLVM C API的讀後感,不過用我自己的方式表達。
我重寫了程式碼,放在整篇文章的最後面,先看輸出結果。再回頭看程式碼。
1 | $ cc `llvm-config --cflags` -c sum.c |
這邊可以看兩個部份, Bitcode內容,以及JIT技術。之前介紹過LLVM的Bitcode,利用LLVM API可以生成Bitcode
這邊就不特別提了,原來的連結寫得比較清楚。
1 | LLVMModuleRef mod = LLVMModuleCreateWithName("my_module"); |
看到這篇Type safe handles in C++覺得很有意思。原來可以這樣用。兩個同樣type,不過代表不同意義的Handle,要怎麼區別才安全。
1 | template<class Tag, class impl, impl default_value> |
根據未來的C++ Proposal Toward Opaque Typedefs for C++1Y,用Boost_StrongTypedef可以達到類似的效果
1 | #include <boost/serialization/strong_typedef.hpp> |
其實是看了The C++ Memory Model
之後,對於之前懵懂的點有點茅塞頓開,寫下來記錄。
先來看以下這段Code
1 | int count = 0; |
就直覺上來說,r0拿到的值會是1,而事實往往不會這麼簡單。Compiler有可能把(1)和(2)的指令重排,因為對Single thread來說,如此重排不匯兌結果產生任何影響,如果我們就算強迫Compiler禁止指令重排,CPU也會有機會做這件事。
Singleton這個題目酸燃被出到爛了,不過變化實在千變萬化。列出幾種不錯的解決方式。
1 | class Singleton { |
非常有名的實作方式,在C++11的環境下是Thread-safe的,而C++98沒有這種保證。可以參考Is Meyers implementation of Singleton pattern thread safe?這個討論串。
GCC預設編譯時開啟static threadsafe的選項,所以C++11的程式碼可以正確運行,可以強制使用-fno-threadsafe-statics
關閉這功能。可以參考Are function static variables thread-safe in GCC?
為了很多因素(降低Playform depdent / Optimization等。 GCC 跟 CLANG 都引進了一層中間層,這曾的目的是定義一個平台無關的指令集, 以老朋友hello來示範如何輸出中間產物。
1 | #include <stdio.h> |
看到濟濟篇文章的總結,寫起來,免得忘了。
以下這段Code應該一堆人猜錯
1 | int i = 0; |
continue
會跳到邏輯判斷的地方,而不是Block的最前端。
sizeof是compile-time的operator,所以以下這段code的結果會是4 0
1 | int i = 0; |
另外sizeof在不同的地方會有不同的表現
1 | int arr[SIZE][SIZE][SIZE]; |
裡面最令人驚奇的是print_sizeof中的第一個輸出值是4,因為第一維的index是pointer。
1 | class A { |
這邊的輸出結果沒有呼叫A的Constructor,因為a只宣告沒定義。
需要加上
1 | A B::a; |
如果我們沒有加上定義,這樣子是沒有問題的。
1 | class B { |
因為A是個empty class
所以就算沒定義也沒問題,如果不是的話就會出現編譯錯誤。
這段Code沒有問題
1 | class A { |
因為member function不涉及這物件的屬性任何操作,因此什麼都沒發生。
參考docker-gitlab這個網頁,實際餐作一次。
1 | $ docker pull sameersbn/gitlab:latest |
1 | $ |
紀錄一下STL裡面常用的幾種iterator
顧名思義就是將容器從後面往前的順序訪問。
1 | #include <iterator> |
共分三種
vector deque list
deque list
1 | #include <iterator> |
C++11之後新增的,顧名思義就是加上move semantics,給個範例就很明顯了。
1 | #include <iostream> // std::cout |