Posted on October 29th, 2008 at 15:49 by fr3@K
今天從 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 的支持:
- Lambda Expressions (functional programming 來了, C++ 請接招)
- Rvalue References (opportunity for major performance boost)
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.