您知道如何执行字符串的惰性求值,就像这个D语言片段中的那样:
void log(lazy string msg) {
static if (fooBarCondition)
writefln(…) /* something with msg */
}
实际上,这个问题可能根本不需要懒惰模式,因为它是一个静态的if语句。也许可以在没有使用时丢弃char const*
字符串?比如,在C++中:
void log(char const *msg) {
#ifdef DEBUG
cout << … << endl; /* something with msg */
#else /* nothing at all */
#endif
}
有什么想法吗?谢谢。
static if
看起来很有吸引力。 - Captain Obvliouslazy
是什么意思...它意味着,如果static_if为false,则不必计算调用表达式。在C++11中,您可以通过将lambda表达式作为参数来获得相同的效果(但不如D语言中的实现干净)。 - Jim Balter