我正在学习 boost::lambda 并且我已经设法创造了一个我目前所知道的无法解决的情况。
显然,在 boost::lambda 的内部,以下示例导致尝试实例化抽象类 AbstractFoo,并阻止 lambda 表达式编译。问题是我不知道它为什么要实例化它,所以我无法尝试解决它。
任何能够:
- 告诉我为什么会这样?
- 建议解决方法?
例子:
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
struct AbstractFoo
{
typedef boost::shared_ptr<AbstractFoo> Ptr;
virtual int it() const = 0;
};
struct Bar : public AbstractFoo
{
typedef boost::shared_ptr<Bar> Ptr;
virtual int it() const { return 3; }
};
typedef AbstractFoo Foo; // Comment this out
//typedef Bar Foo; // and this in to make this example compilable
int main()
{
namespace bll = boost::lambda;
boost::function< bool (const Foo::Ptr &)> func;
func = (bll::protect(bll::bind( &Foo::it, *bll::_1))(bll::_1) == 3);
return 0;
}
这无法编译(在 gcc 4.4.3 上,boost 1_40)并出现怪物模板错误,其中重要的部分似乎是:
error: cannot declare field
‘boost::tuples::cons<AbstractFoo,boost::tuples::null_type>::head’
to be of abstract type ‘AbstractFoo’
because the following virtual functions are pure within ‘AbstractFoo’:
virtual int AbstractFoo::it() const