今天上google, 结果搜到 codeproject 上的一个关于写Windows 注册库的文章 http://www.codeproject.com/system/registry_value.asp#xx1111845xx 真正大开眼界, 其中使用的模板技术很"直白", 不想boost那样复杂, 但是又很好地和实际开发结合起来了, 结果在代码中写注册库及其简单, 估计即使是开发一个script语言专门用于写registry, 也不过如此. 例如, 有一个Point结构, 需要写入注册库中, C++代码就是以下几行.
typedef registry_binary<POINT> regpoint; regpoint pnt("Software\\RegValueTest\\blah", HKEY_CURRENT_USER); POINT p = {99,88}; pnt = p; //写入注册库 POINT stored = p; //从注册库中读. 写注册库本身并没有太多意思, 关键是, 这种不轻易间流露出来的C++的优雅.
以前看过bjarne的一个例子, lambda可能大家都知道, boost中的实现及其复杂. 不过如果你不要搞那么"工业强度", 要作一个简单的那就真是漂亮. 例如 struct lambda {}; template<class T> struct less_than { less_than(const T& c) : comp(c) { } bool operator()(const T& t) const { return t < comp; } T comp; }; //关键在这里, 重载operator< , 不返回一个bool, 而是返回一个Functor template<class T> less_than<T> operator < (lambda l, const T& t)
{ return less_than<T>(t); } 使用起来也很简单, int main() { vector<int> vi; ... lambda l; cout << count_if(vi.begin(), vi.end(), l < 2); return 0; }
|