如何限定模板参数,使其是某个类的派生?

muddogxp 2005-12-07 02:24:22
class base {
.......

};

template < class subclass > // 希望这里的subclass是base的派生类
function() {
........
}
...全文
343 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
noneone 2005-12-08
  • 打赏
  • 举报
回复
学习
muddogxp 2005-12-07
  • 打赏
  • 举报
回复
不错
sinall 2005-12-07
  • 打赏
  • 举报
回复
向楼上学boost。
pongba 2005-12-07
  • 打赏
  • 举报
回复
哦,我看到你说的是函数,那也一样,如下:

// declarative way, overloadable!
template<typename T>
void foo(T t, typename boost::enable_if< boost::is_base_of<B,T>,int >::type = 0)
{
// do your business here!
}

// imperative way, can't be used in overload resolution!
template<typename T>
void foo1(T t)
{
BOOST_STATIC_ASSERT( (boost::is_base_of<B,T>::value) );

// do your business here!
}

int main()
{
foo(D()); // ok
foo(X()); // error!

foo1(D()); // ok
foo1(X()); // error!

return 0;
}
pongba 2005-12-07
  • 打赏
  • 举报
回复
两种方法;一种是declarative的,另一种是imperative的。
代码如下:

// if you don't have a boost library, download it here: http://www.boost.org
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <boost/static_assert.hpp>

struct B{};
struct D:B{};
struct X{};

// declarative way
template<typename T,typename Enable = void>
struct C;

template<typename T>
struct C<T,typename boost::enable_if<boost::is_base_of<B,T> >::type >
{
// do your business here!
};

// imperative way
template<typename T>
struct C1
{
private:
BOOST_STATIC_ASSERT( (boost::is_base_of<B,T>::value) );
public:
// do your business here!
};

int main()
{
C<D> c1; // ok
C<X> c2; // error!

C1<D> c3; // ok
C1<X> c4; // error!
return 0;
}

--
C++的罗浮宫
http://blog.csdn.net/pongba
xlsue 2005-12-07
  • 打赏
  • 举报
回复
刚开始的时候,我以为我的另个办法可以,试了一下。行不通。
我现在的功力还不够。目前只知道这个办法,在这个的基础上,还可以使用define,减少手工的特化。。。
积木 2005-12-07
  • 打赏
  • 举报
回复
编译上面的技巧。。。偶是想不出来了。
muddogxp 2005-12-07
  • 打赏
  • 举报
回复
我只是想限定。如果派生很多,都特化的化就没必要了
xlsue 2005-12-07
  • 打赏
  • 举报
回复
我想不出其它办法,我的办法是有多少个继承类就多少个特化。
#include <iostream>
#include <string>
using namespace std;

class base{ };
class derived1:public base { };

template <typename subclass > // 希望这里的subclass是base的派生类
void function();

template <>
void function<base>() {

}

template <>
void function<derived1>() { }

int main()
{
//function<int>();
function<base>();
function<derived1>();

return 0;
}
muddogxp 2005-12-07
  • 打赏
  • 举报
回复
想达到的效果是:
除了base的派生类外,其他类型作为参数,编译不过
muddogxp 2005-12-07
  • 打赏
  • 举报
回复
特化能解决问题马?
我只是想限定subclass这个类型是base的派生,
也就是说subclass可以是所有base派生类型。
muddogxp 2005-12-07
  • 打赏
  • 举报
回复
如何特化?给个例子。
dragonzxh误解我的意思了
xlsue 2005-12-07
  • 打赏
  • 举报
回复
回复人: goodboy1881(积木) ( ) 信誉:110 2005-12-07 14:38:00 得分: 0


直接一个特化搞定所有问题。
------------------------------------------------------------------
同意。。。

dragonzxh 2005-12-07
  • 打赏
  • 举报
回复
不太懂楼主意思
template<class base>
class subclass:public base
{}
???
积木 2005-12-07
  • 打赏
  • 举报
回复
直接一个特化搞定所有问题。
muddogxp 2005-12-07
  • 打赏
  • 举报
回复
包括的
xlsue 2005-12-07
  • 打赏
  • 举报
回复
不包括base?

64,661

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧