c – 由于抽象模板arg的实例化,boost :: lambda表达式无法编译.任何解释和或解决方法?

c – 由于抽象模板arg的实例化,boost :: lambda表达式无法编译.任何解释和或解决方法?,第1张

概述我正在学习boost :: lambda,我设法创造了一个我目前无法解决的情况. 显然在boost :: lambda的内容中,以下示例导致尝试实例化抽象类AbstractFoo,并阻止lambda表达式编译.问题是我不知道它为什么试图实例化它所以我不能尝试解决它. 任何boost :: lambda专家都可以: >给我一个线索,说明为什么会这样? >建议一个解决方案? 例: #include < 我正在学习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 compilableint 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 compilableint 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类型的对象

总结

以上是内存溢出为你收集整理的c – 由于抽象模板arg的实例化,boost :: lambda表达式无法编译.任何解释和/或解决方法?全部内容,希望文章能够帮你解决c – 由于抽象模板arg的实例化,boost :: lambda表达式无法编译.任何解释和/或解决方法?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1229598.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存