简体   繁体   English

C ++ / Boost模板运行时多态

[英]C++/Boost template runtime polymorphism

Not sure how to state subject clearly. 不确定如何清楚地陈述主题。

Suppose I have a bunch of functor classes which provide some method. 假设我有一堆函子类提供了一些方法。 Now I want to create a proxy class which will redirect the method call to one of the underlying functors. 现在,我想创建一个代理类,它将方法调用重定向到底层函子之一。

eg: 例如:

template<class F>
class proxy_impl : proxy {
    F f;
    void method() { f.method(); }
};

template<>
class proxy_impl<void> {
    virtual void method() = 0;
};

class proxy {
    auto_ptr<proxy_impl<void> > *impl_;
    template<class F>
    proxy(F f) : impl_(new proxy_impl<F>(f)) {}    
    void method() {
        impl->method();
    }
};

What is this pattern called and does boost have implementation? 此模式称为什么,boost是否已实现?

The reason for not having functors inherit directly is because functors can be something like a nameless lambda expression. 没有函子直接继承的原因是因为函子可能类似于无名的lambda表达式。

Ok, so it seems I need something like boost::any and boost::function functionality in one. 好的,看来我需要像boost :: any和boost :: function这样的功能。

It looks like you're trying to re-invent object-based polymorphism... badly. 看来您正在尝试重新发明基于对象的多态性。

here'sow to do what you want 在这里做你想做的

class interface { virtual void method()=0; }
class impl1 : public interface { void method(); }
class impl2 : public interface { void method(); }

...//example usage
interface i *;
if (cond) i = new impl1(); else i= new impl2();
i->method();//calls whichever implementing method was constructed.

看起来很像Boost.Function

As you suggest, this can be done with boost.any and boost.function. 如您所建议,这可以通过boost.any和boost.function完成。 Specifically: 特别:

struct proxy {
  template <typename T>
  proxy(T t) : obj_(t) {
    method = [&obj_] { boost::any_cast<T&>(obj_).method(); }
  }
  boost::function<void()> method;
private:
  boost::any obj_;
};

If .method() is const, then you can do away with the boost::any, and just have the lambda capture the T object by value. 如果.method()是const,则可以取消boost :: any,而让lambda通过值捕获T对象。 In fact, in that case you could just do away with the proxy object and just use a bare boost::function. 实际上,在这种情况下,您可以删除代理对象,而只使用裸露的boost :: function。

I don't think I really understand.. It seems that all you want is to be able to chain functors together: 我认为我不太了解。似乎您想要的只是能够将函子链接在一起:

struct null_functor { void method() { }; };

template <typename F = null_functor>
struct functor1 {
  F f;
  void method() { 
    std::cout << "functor1 called!" << std::endl;
    f.method();
  };
};

template <typename F = null_functor>
struct functor2 {
  F f;
  void method() { 
    std::cout << "functor2 called!" << std::endl;
    f.method();
  };
};

int main() {
  functor1 f1;
  f1.method();

  functor1< functor1 > f11;
  f11.method();

  functor2< functor1 > f21;
  f21.method();

  return 0;
};

If you need dynamic binding on top of that, just make one functor be a base class with a virtual method, and derive other functors from it. 如果您还需要动态绑定,只需使一个仿函数成为具有虚拟方法的基类,然后从该仿函数派生其他仿函数。 You could use Boost.Bind as well. 您也可以使用Boost.Bind。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM