跳到主要內容

發表文章

C++ 明確樣板具現化 (C++ Explicit Template Instantiation)

最近的文章

Migrate OSes of AMD drivers to Non-AMD Hardware

Migrate OSes of AMD drivers to Non-AMD Hardware Scenario A bootable disk of GRUB multi-boot configuration for Ubuntu Desktop 20.04 and Windows 10. Migrate the disk from machine A to B, where, Machine A has AMD APU A10 5800K and AMD RX570 graphics card installed as well as related drivers. Machine B has Intel i5 CPU and Nvidia 3060 graphics card installed. Issue Both Windows 10 and Ubuntu failed to boot normally. Ubuntu displayed black screen and Windows 10 displayed BSOD within AODDriver2.sys errors. Solution The main goal is removing the problematic driver. Thus we need to boot without loading the driver. Following are steps for both OSes. Ubuntu Select Advanced Options for Ubuntu . Select latest kernel version of (recovery mode) suffix. Select Enable network (may not necessary, I did it anyway). Select resume . Such that Ubuntu successfully boots into desktop environment without loading the problematic graphics driver. For now screen resolution

Breakpad on Windows

Breakpad on Windows Exception Handling Windows’ SEH can be handled with compiler extension __try {} __exception() {} or by setting top-level handler via SetUnhandledExceptionFilter() . The latter is tricky since it’s easy to replace a previous registered handler whenever a module or library wants to. Breakpad Breakpad implements ExceptionHandler for Windows. It registers an exception handler that capable of dump minidump to a specified directory. It uses, unfortunately, SetUnhandledExceptionFilter() and potentially fail to capture an exception if another handler was registered after Breakpad. Solution We are left with __try {} __exception() {} as follows. using namespace google_breakpad ; auto handler = ExceptionHandler ( L "dumps" , nullptr , nullptr , nullptr , ExceptionHandler :: HANDLER_NONE ) ; __try { // ... } __except ( ( handler . WriteMinidumpForException ( :: GetExceptionInformation ( ) , EXCEPTION_EXECUTE_HANDLER

Ebooks Everywhere

Ebooks Everywhere Requirements Calibre with De vice Digital Reader Manager plugin 1 Adobe Digital Editions Steps Export books from where you brought it as ACSM for EPUBS Add above ACSM files to Adobe Digital Editions Wait for finishing downloads Add downloaded EPUBs to Calibre zzzz ↩︎

得利油漆色卡編碼方式

得利油漆色卡編碼方式 類似 Munsell 色彩系統 ,編碼方式為 HUE LRV/CHROMA 例如 10GY 61/449 ( 色卡 ) 編碼數值 描述 10GY hue ,色輪上從 Y(ellow) 到 G(reen) 區分為 0 ~ 99 ,數值越小越靠近 Y,越大越靠近 G 61 LRV (Light Reflectance Value) 塗料反射光源的比率,數值從 0% ~ 100% ,越高越亮,反之越暗,也可理解為明度 449 chroma 可理解為彩度,數值沒有上限,越高顏色純度 (濃度) 越高 取決於測量儀器,對應至 RGB 並不保證視覺感受相同。 參考資料: 色卡對照網站 e-paint.co.uk Written with StackEdit .

Stringify C++ Enum Safely

Stringify Enum Safely Sometimes stringifying C/C++ enumerator, or enum , values within its literal names are useful for debugging or logging. In this article, I’ll share how I achieve basic enum stringify and how to do it safely against changes to enum definitions. Basic Idea Given a boring enum Result as follows. enum class Result : int8_t { OK = 0 , FAILED , } ; My usual way to stringify them is using a table, i.e. # define STR(X) #X char const * StringifyResult ( Result res ) { char const * str [ ] = { STR ( OK ) , STR ( FAILED ) , } ; return str [ static_cast < std :: underlying_type_t < Result >> ( res ) ] ; } int main ( ) { printf ( "%s" , StringifyResult ( Result :: OK ) ) ; // print "OK" } It benefits us from simplicity - simply copy’n paste those enum values then decorate them with STR() . Tons of editors can accomplish such editing in a snap. Check Consistency

Devirtualization and TDD

Devirtualization and TDD Devirtualization (去虛擬化) Devirtualization 是 C++ 編譯器的一種最佳化,舉個例子: struct foo { virtual ~ foo ( ) { } virtual int bar ( ) = 0 ; } ; struct food : foo { int bar ( ) final { return 2 ; } } ; struct fool : foo { int bar ( ) override { return 3 ; } } ; int test ( food & f ) { return f . bar ( ) ; } int test ( fool & f ) { return f . bar ( ) ; } test(food& f) 用 -O2 編譯後產生的組語是 test(food&): mov eax, 2 ret 而 test(fool& f) 則產生 test(fool&): mov rax, QWORD PTR [rdi] mov rax, QWORD PTR [rax+16] cmp rax, OFFSET FLAT:fool::bar() jne .L6 mov eax, 3 ret .L6: jmp rax 差別在於前者用了 final 而後者使用 override , final 可以告訴編譯器 food::bar 不會再被衍生型別覆寫,因此不需要查找 vtable 而直接呼叫 food::bar 。 final 也可以用來敘明類別不會被其他類別繼承。例如: struct foot final : foo { /* impl */ } ; 以上可以發現,能套用 devirtualization 的

std::optional 的未來?

std::optional<T&> 的未來? 前言 春節就是拋拋走,趁著搭車的時間看完 JeanHeyd Meneide (ThePhantomDerpstorm/ThePhD) 的 血淚控訴 To Bind and Loose a Reference 1 ,他聲稱:標準委員會因為一個不存在的對手,訂定了一個殘缺的 std::optional 。文章非常值得一讀,寫不出配得上的心得,只好寫一篇筆記。 Nullable 型別 C++17 增添了 std::optional<T> ,C++20 加入了 std::variant<Ts...> ,C++2x 預計會出現 std::expected<T> ,這些工具的相同點在於他們都是 nullable 型別 ─ 物件狀態可以是含有 T 或者不含有 T (也就是 null),雖然應用在不同的情境上,但某種程度上都可以理解為特化版的指標。 Reference 有甚麼問題? std::optional 在 cppreference.com 裡的描述 2 裡面有一句話: There are no optional references; a program is ill-formed if it instantiates an optional with a reference type. 同樣適用於 std::variant 、 std::expected 。其實在 Boost.Optional 或是 Boost.Variant 都是支援 reference 型別的,然而 C++ 標準委員會對下面這段程式分裂成兩派不同的見解 (範例取自 JeanHeyd Meneide): # include <optional> # include <iostream> int main ( int , char * [ ] ) { int x = 5 ; int y = 24 ; std :: optional < int & > opt = x ; opt = y ; std :: cout <

C++17 新功能 try_emplace

C++17 新功能 try_emplace 回顧 emplace 大家的好朋友 Standard Template Library (STL) 容器提供如 push_back , insert 等介面,讓我們塞東西進去; C++11 之後,新增了 emplace 系列的介面,如 std::vector::emplace_back , std::map::emplace 等,差異在於 emplace 是在容器內 in-place 直接建構新元素,而不像 push_back 在傳遞參數前建構,下面用實例來說明: struct Value { // ctor1 Value ( int size ) : array ( new char [ size ] ) , size ( size ) { printf ( "ctor1: %d\n" , size ) ; } // ctor2 Value ( const Value & v ) : array ( new char [ v . size ] ) , size ( v . size ) { printf ( "ctor2: %d\n" , size ) ; memcpy ( array . get ( ) , v . array . get ( ) , size ) ; } private : std :: unique_ptr < char [ ] > array ; int size = 0 ; } ; struct Value 定義了自訂建構子 (ctor1),以指定大小 size 配置陣列,複製建構子 (ctor2) 則會配置與來源相同大小及內容的陣列,為了方便觀察加了一些 printf 。當我們如下使用 std::vector::push_back 時 std :: vector < Value > v ; v . push_back ( Value ( 2048 ) ) ; 首先 Value 會先呼叫 ctor1,傳給 push_ba

KNN Text Classification

KNN Text Classification Simplified Data Set Assume terms a document are unique (tag-like dataset). doc terms d0 [ t0, t2, t3 ] d1 [ t1, t3 ] d2 [ t0, t2 ] and docs = [ d0, d1, d2 ] d0.terms = [ t0, t2, t3 ] Weight of Terms Represent weight of terms with a term-by-document Matrix, A, where ti denotes term i and dj denotes document j . Entry A(i, j) is computed by TF-IDF . For example: A(0, 0) = (1 / # of terms of d0) * log2(# of docs / t0 occurrences for all docs) = (1/3) * log2(3/2) ~= 0.195 Hence A of the dataset is term\doc d0 d1 d2 t0 0.195 0 0.292 t1 0 0.792 0 t2 0.195 0 0.292 t3 0.195 0.292 0 Note : t0 … t3 can be viewed as a multi-dimension space such that d0 is a point (0.195, 0, 0.195, 0.195) of the space. Document Similarity Per the weight matrix we can compute document-to-document similarities matrix, S, by cosine-similarity. For example: intersect(d0.terms, d0.terms) =

PST: Fast Float Serialization

PST: Fast Float Serialization Given float numbers of precision 3 (rounded to decimal places), e.g. 0.123. Following method can de/serialize float numbers safely across platforms of different endianness. // Serialization float number [ 100 ] ; std :: ofstream fout ( “output” ) ; for ( int i = 0 ; i < 100 ; ++ i ) fout << ( number * 1000 ) << endl ; // De-serialization int value ; float result [ 100 ] ; std :: ifstream fin ( “input” ) ; for ( int i = 0 ; i < 100 ; ++ i ) { fin >> value ; result [ i ] = value / 1000.0 ; } By translating float number to a discrete form, the cost of de/serialization (text to double) is reduced since it becomes a text to integer conversion. Original posted in my Tumblr Update: Another technique union { int ival ; float fval ; } number ; number . fval = 0.1234 ; fout << number . ival ; fout >> number . ival ; // use number.fval –

Getting Started with Google Test and CMake on Windows

Getting Started with Google Test and CMake on Windows Versions Windows 7 MSVC 11 (Visual Studio 2012) CMake 3.0 gtest 1.7.0 Google Test Assume we placed unzipped gtest source in %GTEST_ROOT% . To get gtest-1.7.0 compiled with MSVC11, edit %GTEST_ROOT%\cmake\internal_utils.cmake . macro ( fix_default_compiler_settings_ ) $ if ( MSVC ) $ add_definitions ( - D_VARIADIC_MAX = 10 ) # Add this line # . . . endif ( ) endmacro ( ) Note : You won’t need the workaround with MSVC12, or VS2013 (I envy you). Open up “Developer Command Prompt for VS2012” (yeah, I LOVE cmdline) and type: # Compile cd %GTEST_ROOT% mkdir build cd build cmake .. -G "NMake Makefiles" # Install with prefix "c:\opt" ( change it to whatever you like ) mkdir c:\opt\lib mkdir c:\opt\include xcopy *.lib c:\opt\lib cd .. xcopy include\gtest c:\opt\include\ Note : I actually did files/dirs copying in cygwin and welcome feedback for