C++类型擦除与`std::function`性能探索

什么是类型擦除

对于Python这种动态类型语言来说,是不存在“类型擦除”这个概念的。Python对象的行为并不由接口定义,而是由“当前方法和属性的集合”决定。

所以,以下的代码是完全合法的:

class Foo(object):
    def print(self):
        print 'foo'

class Bar(object):
    def print(self):
        print 'bar'

def do_print(obj):
    print obj.print()

do_print(Foo()) // print 'foo'
do_print(Bar()) // print 'bar'

但是对于C++这种静态类型语言来讲,我们就不能使用这种语法:

class Foo {
public:
    void print() { cout …
more ...