Strong Guarantee using Transaction
Posted on April 29th, 2008 at 23:24 by fr3@K

Abrahams Guarantees

在 C++ 的世界裡, 正確的 exception handling 是專業的 C++ programmer 不可或缺的技巧. 雖然它的概念並不困難, 但實作起來卻常不見得那麼容易.

要做到正確的 exception handling, 首先必須要了解什麼是 exception safety. 一個需要與 exception 打交道的 component 可在其介面實作人稱 Abrahams guarantees 的三種 exception safety 保證之一:

  1. The basic guarantee
  2. 允許操作失敗時改變物件的狀態, 但不能有 resource leak. 且該物件的狀態必須是可靠的仍然可以被解構, 操作失敗後該物件的狀態可以是不完全能被預測的.

  3. The strong guarantee
  4. 操作後的狀態只能是成功完成, 或是將該物件回復到操作之前的狀態並拋出一個 exception.

  5. The no-throw guarantee
  6. 操作不會拋出 exception.

(more…)

del.icio.us:Strong Guarantee using Transaction digg:Strong Guarantee using Transaction spurl:Strong Guarantee using Transaction newsvine:Strong Guarantee using Transaction furl:Strong Guarantee using Transaction Y!:Strong Guarantee using Transaction 黑米共享書籤:Strong Guarantee using Transaction 推推王:Strong Guarantee using Transaction
五種寫 For Loop 的方法
Posted on April 11th, 2008 at 6:23 by fr3@K

同樣是寫一個迭代的 loop, 至少有下面兩種標準 (C++98), 兩種半標準 (Boost), 以及一種未來式 (C++0x) 的寫法:
(more…)

del.icio.us:五種寫 For Loop 的方法 digg:五種寫 For Loop 的方法 spurl:五種寫 For Loop 的方法 newsvine:五種寫 For Loop 的方法 furl:五種寫 For Loop 的方法 Y!:五種寫 For Loop 的方法 黑米共享書籤:五種寫 For Loop 的方法 推推王:五種寫 For Loop 的方法
Avoid Pointer Parameters and Inheritance
Posted on May 22nd, 2007 at 23:16 by fr3@K

Preamble

Microsoft 的 MFC 是最早被大量採用 (massive adoption) 的 C++ library 之一. 等到我開始接觸 C++ Standard Library 這東西都已經是玩了兩年 MFC 以後的事. 還記得, 從一開始對 MFC 的讚嘆與擁抱, 幾年後對它的不屑, 到更後來的理解 (理解不好其實也是有原因的).

即便不少 programmer 知道 MFC 是一套瑕疵遍佈的 library, 可能也知道那些地方有問題. 但它以及部份其他 library 聯手對於更多 C++ programmer 造成的傷害已經留下不容易抹滅的痕跡. 它讓許多 programmer 以為這些東西本來就該這樣 (that’s the way things supposed to be), 當有一天這些被誤導的 programmer 有機會可以選擇另一套 library 或是自行設計的時候, 很容易就陷入 MFC 帶給他們已先入為主的錯誤觀念.
(more…)

del.icio.us:Avoid Pointer Parameters and Inheritance digg:Avoid Pointer Parameters and Inheritance spurl:Avoid Pointer Parameters and Inheritance newsvine:Avoid Pointer Parameters and Inheritance furl:Avoid Pointer Parameters and Inheritance Y!:Avoid Pointer Parameters and Inheritance 黑米共享書籤:Avoid Pointer Parameters and Inheritance 推推王:Avoid Pointer Parameters and Inheritance
(How do you) lower (or upper) your strings, the STL way
Posted on March 12th, 2007 at 21:41 by fr3@K

一直以來, 對 C++ Standard Library 裡面沒有字串大小寫轉換的 function, 除了納悶還是納悶. 在 <algorithm> 裡面沒有, 即便是提供了典型 fat interfacestd::basic_string<> , 也獨漏 upper/lower.

(more…)

del.icio.us:(How do you) lower (or upper) your strings, the STL way digg:(How do you) lower (or upper) your strings, the STL way spurl:(How do you) lower (or upper) your strings, the STL way newsvine:(How do you) lower (or upper) your strings, the STL way furl:(How do you) lower (or upper) your strings, the STL way Y!:(How do you) lower (or upper) your strings, the STL way 黑米共享書籤:(How do you) lower (or upper) your strings, the STL way 推推王:(How do you) lower (or upper) your strings, the STL way
Boost.Lambda
Posted on September 29th, 2006 at 20:32 by fr3@K

STL 提供了許多實用的 algorithm. 例如 for_each, 可將一個 functor 施用於一串 iterator 所 dereference 後的 instance:

    // Example A
    struct output_to_console_followed_by_a_space
    {
        template 
        void operator () (const T& instance) const
            {
                cout << instance << ' ';
            }
    };
    
    void foo (const vector& values)
    {
        // do a few things first…
    
        // some lines of code
    
        // some more code
    
        for_each(
            values.begin(),
            values.end(),
            output_to_console_followed_by_a_space());
    }
    

許多時候, 在 algorithm 中要對 iterator dereference 後得到的 instance 做的事情很 trivial. 就像例 A 一樣寫個 functor 一點都不困難, 可是卻有幾個地方很討厭.
(more…)

del.icio.us:Boost.Lambda digg:Boost.Lambda spurl:Boost.Lambda newsvine:Boost.Lambda furl:Boost.Lambda Y!:Boost.Lambda 黑米共享書籤:Boost.Lambda 推推王:Boost.Lambda