pha*_*zon 3 c++ d lazy-evaluation c++11
你知道如何对字符串进行延迟评估,就像在这个D片段中一样:
void log(lazy string msg) {
static if (fooBarCondition)
writefln(…) /* something with msg */
}
Run Code Online (Sandbox Code Playgroud)
实际上,问题可能根本不需要懒惰,因为静态 if.也许有可能char const*在不使用时丢弃字符串?就像,在C++中:
void log(char const *msg) {
#ifdef DEBUG
cout << … << endl; /* something with msg */
#else /* nothing at all */
#endif
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?谢谢.
#ifdef DEBUG
#define log(msg) do { cout << … << endl; } while(0)
#else
#define log(msg) do { } while(0)
#endif
Run Code Online (Sandbox Code Playgroud)
在C++ 11中有两种方法可以实现懒惰:宏和lambda表达式.两者在技术上都不是"懒惰",而是所谓的"正常评估"(与"急切评估"相对),这意味着表达式可能被评估任意次.因此,如果要将程序从D(或haskell)转换为C++,则必须注意不要在这些表达式中使用具有副作用(包括计算时间)的表达式.
要实现真正的懒惰,你必须实现memoizing,这不是那么简单.
对于简单的日志记录,宏很好.
| 归档时间: |
|
| 查看次数: |
1174 次 |
| 最近记录: |