c – 由于抽象模板arg的实例化,boost :: lambda表达式无法编译.
发布时间:2020-12-16 07:26:23 所属栏目:百科 来源:网络整理
导读:我正在学习boost :: lambda,我设法创造了一个我目前无法解决的情况. 显然在boost :: lambda的内容中,以下示例导致尝试实例化抽象类AbstractFoo,并阻止lambda表达式编译.问题是我不知道它为什么试图实例化它所以我不能尝试解决它. 任何boost :: lambda专家都
我正在学习boost :: lambda,我设法创造了一个我目前无法解决的情况.
显然在boost :: lambda的内容中,以下示例导致尝试实例化抽象类AbstractFoo,并阻止lambda表达式编译.问题是我不知道它为什么试图实例化它所以我不能尝试解决它. 任何boost :: 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 解决方法
正如您所发现的那样,您不能这样做,因为需要复制对象,但在这种情况下,它无法实例化,因为它包含纯虚方法.最简单的解决方案是使用指针传递它:
#include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #include <boost/shared_ptr.hpp> #include <boost/function.hpp> #include <iostream> 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 * )> func; func = ( bll::protect( bll::bind( &Foo::it,bll::_1 ) )( bll::_1 ) == 3); //func = bll::bind( &Foo::it,bll::_1 ); Foo::Ptr p( new Bar ); std::cout << std::boolalpha << func( p.get() ) << std::endl; } 更准确地说,这个: *bll::_1 需要实例化和复制AbstractFoo类型的对象 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |