Posted on August 9th, 2009 at 19:24 by fr3@K
懶人包 + Bjarne on DDJ (翻譯), from Paramecium.
Great job! Thanks, Keiko.
懶人包 + Bjarne on DDJ (翻譯), from Paramecium.
Great job! Thanks, Keiko.
幾個月前曾在一篇文字裡寫道: 正確的運用 Rvalue References, 有可能帶來可觀的 performance gain. 前幾天看到 Visual C++ Team Blog 新貼出對 STL (VS2010 vs. VS2008) 的 performance benchmark. 在一個程度上, 該篇文字可以說驗證了這個不只我一個人獨有的看法與對 C++0x 的期待.
Rvalue-References 帶來的 performance boost 或許比不上 Multi-Core STL 來得那麼 “方便”, 但絕對是你不會想要錯過的 low hanging fruit.
C++0x 的巨浪就要來了, 你準備好了嗎?
喜愛 C++ 並關心 C++0x 標準的朋友一定會知道, C++0x 引進了許多新的元素. 不但對 Standard Library 有為數眾多的 addition 與 enhancement, 連 core language 都新增了不少東西. 而其中最重要的, 在我看來, 就是 Rvalue References. 即便不理會 C++0x 其他的新玩意 Rvalue References 依然是一個職業 C++ programmer 該要掌握的新 feature. 原因很簡單, 正確的運用 Rvalue References, 有可能帶來可觀的 performance gain.
(more…)
Keiko 寫了篇 討論 DSL 的文字. 這篇文字不短, 有層次. 有他說的 “學院派式” 的格調. 讓我有一種在看一份探討輪胎直徑與胎壓以及胎壁厚度之間關係的 paper 的 feel. 相形之下, 這裡寫的東西看起來更像是黑手示範如何補胎換胎的 howto.
好了, 細節不多談, 建議看官移架拜讀. 重點是, 本要在 comment 裏寫 code, 但這該死的 blogger 超難用, 讓我想起當年 從 blogger 離家出走 的原因. 又扯遠了, 回到我想回應給 Keiko 的東西.
(more…)
Rvalue-References 是 C++0x 規範的新 language feature, 也是繼 Templates 後, IMO, C++ 語言最重要的 language feature. 其目的是為了支持 Move Semantics — 將 resource 從一個 instance 轉移到另一個 instance.
Rvalue — 這個聽起來饒舌的新名詞 — 說穿了其實就是 un-named instance (i.e. temporary), 反之, 有名字的 (named) 則為 lvalue. 而 const/volatile 與否則是額外的修飾. 也就是說, 一個 rvalue reference 其實就是 reference to a temporary.
(more…)
今天從 Sutter’s Mill 知道 最新的 C++0x draft (n2798) 已經正式 publish 以被 review 與 comment. Sutter 稱這 draft 為 feature complete.
也是今天, 在 Visual C++ Team Blog 看到 Visual Studio 2010 的 Community Technology Preview (CTP) 版本也 release 了 (按此前往下載頁面. Warning: 11 個 split 過的 rar, 約 7350 MB). 其中最令人興奮的正是對 C++0x 的支持:
void foo()
{
list<string> strings;
// strings.push_back("bar");
strings.push_back(string("bar"));
}
這段 code 將只需要兩次 memory allocation, 一次在 list, 一次在 string. 因 "bar"string("bar") (note: see comment from icek) 而產生的 string 暫時物件的 resource 將直接轉移到 list 內部新創建的 element 身上 (move-construct). 不再是把該暫時物件 copy 到新創建的 element 身上 (copy-construct).
Note: 雖然 VS10 的 C++ compiler 已經支援 Rvalue-References, 但由於 VS10 所 ship 的 STL (by Dinkumware) 尚未實現 Rvalue-References 的部份, 因此, 在 VC10 的 STL 改善之前, 上面的 code snip 仍然會觸發 string 的 copy-construction, 換句話說會引發三次 allocation.
template <class Container>
void bar(const Container& cont)
{
for(auto first = cont.begin(); first != cont.end(); ++first)
{
// do meaningful things here...
}
}
別把用在此處的 auto 與 variant 混為一談, 兩者完全不同. 後者 (variant) 的型別 (或者說意義) 是 runtime 決定的 (可參考 Boost.Variant 與 COM 的 variant). 前者 (auto) 則是在 compile time 就決定了. 簡單的說, 是 compiler 在 compile 時藉由後面的敘述 (例中的 cont.begin()) 推導出 auto 定義的變數 (first ) 的 type.
另外, FOSS 陣營的王道 compiler, GCC, 則是一直有在 C++0x 的支持方面持續加強 (GCC 4.3/4.4). 不過 (就我所知) 還沒進入主流的 GNU/Linux distro.
From Sutter’s Mill:
… we already had a nearly-complete C++0x internal working draft — most features that will be part of C++0x had already been “checked in.” Only a few were still waiting to become stable enough to vote in, including initializer lists, range-based for loops, and concepts.
在 C++ 的世界裡, 正確的 exception handling 是專業的 C++ programmer 不可或缺的技巧. 雖然它的概念並不困難, 但實作起來卻常不見得那麼容易.
要做到正確的 exception handling, 首先必須要了解什麼是 exception safety. 一個需要與 exception 打交道的 component 可在其介面實作人稱 Abrahams guarantees 的三種 exception safety 保證之一:
允許操作失敗時改變物件的狀態, 但不能有 resource leak. 且該物件的狀態必須是可靠的仍然可以被解構, 操作失敗後該物件的狀態可以是不完全能被預測的.
操作後的狀態只能是成功完成, 或是將該物件回復到操作之前的狀態並拋出一個 exception.
操作不會拋出 exception.
Except where otherwise noted, COdE fr3@K by
fr3@K is licensed under a
Creative Commons Attribution-Share Alike 3.0 License.