直接来自http://herbsutter.com/2013/05/09/gotw-1-solution/
虽然widget w();对我来说很清楚,但我不知道下面的代码如何成为函数声明?
// same problem (gadget and doodad are types)
//
widget w( gadget(), doodad() ); // pitfall: not a variable declaration
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
我今天读过这篇关于lambdas的文章:
http://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood
文章建议,lambda 不是 作为anon内部类实现的(由于性能).它给出了一个示例,可以将lambda表达式编译为类的(静态)方法.
我试过一个非常简单的片段:
private void run() {
System.out.println(this);
giveHello(System.out::println);
}
private void giveHello(Consumer<String> consumer) {
System.out.println(consumer);
consumer.accept("hello");
}
Run Code Online (Sandbox Code Playgroud)
输出是:
sample.Main@14ae5a5
sample.Main$$Lambda$1/168423058@4a574795
hello
Run Code Online (Sandbox Code Playgroud)
所以它不是同一个实例.它不是一些中央的"Lambda Factory"实例.
那么lambdas是如何实现的?
取自:http://herbsutter.com/2013/05/22/gotw-5-solution-overriding-virtual-functions/
我们为什么要写:
auto pb = unique_ptr<base>{ make_unique<derived>() };
Run Code Online (Sandbox Code Playgroud)
而不仅仅是:
auto pb = make_unique<derived>();
Run Code Online (Sandbox Code Playgroud)
我唯一的猜测是,如果我们想要auto,我们需要帮助它推断出正确的类型(base这里).
如果是这样,那么对我来说,这将是真正值得怀疑的优点..键入auto然后在右侧输入很多初始化=..
我错过了什么?
如何编写类型安全的映射器/变换器集合?
class Bean {
public int value;
}
List<Bean> beans = ..
List<Integer> ints = Lib.map(beans, b => b.value);
Run Code Online (Sandbox Code Playgroud)
使用Apache Commons-Collections它看起来像这样:
Collection<Integer> ints = CollectionUtils.collect(beans, new Transformer() {
@Override
public Object transform(Object input) {
return null; //cast here, dereference etc.
}
});
Run Code Online (Sandbox Code Playgroud)
但这不是类型安全的