Monthly Archives: July 2017

protected destructor

constructor access modifier 一般設為public,而在singleton中,為了限制生成的物件個數,會將constructor放在private,另外在其他method生成物件。而對於destructor,以Meyers Singleton的實作來說,destructor放在public or non-public皆可,因為static object 在宣告的地方可以存取到private destructor(參考下面12.4) C++03 std §3.6.3 Termination Destructors (12.4) for initialized objects of static storage duration (declared at block scope or at namespace scope) are called as a result of returning from main … Continue reading

Posted in C++ Language | Leave a comment

object slicing

object slicing發生在衍生類別的物件複製到基類時,預設只複製基類的成員,這時候在衍生類別的資訊會遺失。 這樣有可能造成在執行method或是destructor因為遺失了資訊而造成其他錯誤。 當複製為參考時(base class reference type),則不會有此問題。 object slicing的問題,在exception catch時要特別注意,應該要catch reference(catch(A& a)才不會造成object slicing),而不是catch value 如下面範例: 如果是 catch(A a) 會走copy ctor的路,因為是將B()複製到a,而造成object slicing,並且因為copy後,catch到的物件已經不是當時拋出的那個物件了 參考: https://en.wikipedia.org/wiki/Object_slicing https://stackoverflow.com/questions/274626/what-is-object-slicing http://ptgmedia.pearsoncmg.com/images/0321113586/items/sutter_item73.pdf

Posted in C++ idioms | Leave a comment

std::make_shared

//C++11中的 shared_ptr初始化一般使用 std::shared_ptr p(new A); //但也可以 std::shared_ptr p = std::make_shared(); 使用make_shared可以避免顯式地使用new,另外主要的差別在make_shared 一般會將shared_ptr內的control block和object一起allocate。雖然一次allocate上比較有效率,但如果有用到weak_ptr,當shared count = 0時,不會馬上還回記憶體,因為control block還需要用到,而是要等到沒有任何shared_ptr, weak_ptr參考時,才會釋放記憶體。 在N3690文件 20.9.2.2.6 shared_ptr creation 中提到: Remarks: Implementations should perform no more than one memory allocation. [ Note: This provides efficiency equivalent to … Continue reading

Posted in C++ Language | Leave a comment

wait on thread creation

下面的範例是一般worker thread的做法,利用boost threadgroup,在程式一開始由一個thread進入(main thread),離開的時候也是一個thread 一般來說在創建thread時不需要等待thread被創建完成再往下走,不過這裡示範了當所有thread建立完成後main thread才繼續往下執行(透過condition variable, wait) 這麼做可以讓控制流變的簡潔,並且視thread group的threads為一整體,也能確保在start()完成之前不會執行stop()。 ThreadManager.h #include #include #include #include #include class ThreadManager { std::mutex m; std::condition_variable cv; int numThreads = 5; //non-static data member with initializer (C++11) int startCount = 0; bool bRunning = … Continue reading

Posted in Tips | Leave a comment

warning C4819的處理

warning C4819: The file contains a character that cannot be represented in the current code page (950). Save the file in Unicode format to prevent data loss 選擇 File>Advanced Save Options => Encoding: Unicode (UTF-8 with signature) – Codepage 65001 … Continue reading

Posted in Tips | Leave a comment

VC++ pragma link

在VC++中 #pragma comment(lib, “my.lib”) 在link階段會搜尋 my.lib 一般配合_DEBUG與_MSC_VER 等巨集使用 可以根據編譯的特性來動態決定要link哪一個lib 例如 #if _MSC_VER >= 1400 #pragma comment(lib, “my.lib”) #elif … #endif 使用時建議放在單獨的cpp檔方便查找 MSVC++ 15.0 _MSC_VER == 1910 (Visual Studio 2017) MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015) MSVC++ 12.0 _MSC_VER … Continue reading

Posted in Tips | Leave a comment

boost build (visual studio 2015 winxp toolset)

for winxp support 以上是設定compile需要的參數與搜尋順序 (SDK v7.1A first)接下來build boost (這邊沒有使用icu or iconv) bootstrap.bat b2.exe -sBZIP2_SOURCE=D:\build\bzip2-1.0.6 -sZLIB_SOURCE=D:\build\zlib-1.2.11 toolset=msvc define=WINVER=0x0501 define=_WIN32_WINNT=0x0501 define=NTDDI_VERSION=0x05010000 define=PSAPI_VERSION=1 其他的選項 toolset=msvc link=static,shared (.lib or .dll) threading=multi runtime-link=static,shared (/MT or /MD) variant=debug,release address-model=32 or 64 安裝路徑 install –prefix= 其他注意: 需要安裝SDK … Continue reading

Posted in Library | Leave a comment