有没有办法指定模板参数类型必须从特定基类派生?

问题描述

有没有办法指定模板参数类型必须从特定的基类派生?这就是我希望它起作用的方式:

class  foo
{
    a_foo_function() const;
};
//.......
template  < class F : public foo >  // <<<=== my deceptively creative syntax
class bar
{
    F * pfd;
    bar(F* food){ pfd = food; }
    void f() const {  pfd->a_foo_function();  }  //guaranteed to work in my book
};
//.......
class foo                         just_a_plain_foo;
//.......
class foodish : public foo {}     a_foodish;
//.......
class foolish : public not_foo {} a_foolish;
//.......
class foodous : public foodish {} a_foodous;
//.......
typedef bar<foodish> barf;
barf a(a_foodish); //ok
barf b(a_foodous); //ok
barf c(a_foolish); //NOT ok ... foolish is not derived from foo
barf d(just_a_plain_foo); //NOT ok ... pfd pointer is to derived foodish

是不是很漂亮? 但是我漂亮的语法不会编译;我似乎无法找到如何做到这一点。 请注意,使用基类作为模板参数对我不起作用,因为我可能有从 foo 派生的不同且相互不兼容的类。 TIA。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...