C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 由于抽象模板arg的实例化,boost :: lambda表达式无法编译.任何解释和/或解决方法?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_675_0@
@H_675_0@
我正在学习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类型的对象

@H_675_0@

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:argboostclambda任何实例抽象方法无法模板由于编译表达式解决解释
猜你在找的C&C++相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap