65,186
社区成员




struct Base
{
virtual bool test(int x) = 0;
};
template<typename F>
struct Derived : Base
{
F f;
bool test(int x) {return f(x);}
Derived(F f_) : f(f_) {}
};
template<typename F>
Base* MakeDerived( F f )
{
return new Derived<F>(f);
}
Base* Foo( int k )
{
return MakeDerived( [k](int x) { return x%k==3; } );
//或是:return MakeDerived( [k](int x) -> bool {return x%k == 3;} );
}
bool Bar()
{
Base* b = Foo(3);
return b->test(6);
}