如何强制模板< class>从BaseClassA派生? [英] How to force template <class> to be derived from BaseClassA?

查看:109
本文介绍了如何强制模板< class>从BaseClassA派生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能强迫模板来自某个基类,以便我可以调用基类函数?

Is there any possibility to force a template to be from a certain base class so that I can call the base class function?

template <class T>
void SomeManager::Add(T)
{
    T->CallTsBaseClassFunction();
    //... do other stuff
}


推荐答案

当然,您可以将类型特征与SFINAE结合使用:

Sure, you can combine type traits with SFINAE:

#include <type_traits>

template <class T>
typename std::enable_if<std::is_base_of<your_base_class, T>::value, void>::type
SomeManager::Add(T)
{
    T->CallTsBaseClassFunction();
    //... do other stuff
}

尽管我不真的看不到这里的好处。

Although I don't really see the benefit here.

这篇关于如何强制模板&lt; class&gt;从BaseClassA派生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆