免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2836 | 回复: 2
打印 上一主题 下一主题

[C++] boost function偏特化,请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-25 23:04 |只看该作者 |倒序浏览
下午看了一篇关于boost function 源码剖析的文章(http://dev.yesky.com/196/2012196_1.shtml),上面出现了function 偏特化技术的讲解,自己动手在VC2005做了个测试,发现,按照他预处理后的程序无法编译。
我类似的写了几个模板偏特化,请大家指点。
// 200801.cpp : 定义控制台应用程序的入口点。

//


#include "stdafx.h"
#include<iostream>

using namespace std;

template<typename T1, typename T2, typename Allocate>
class T1_base
{
public:
&nbsp;&nbsp;&nbsp;&nbsp;T1_base()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<<"print T1_base(templeate)"<<endl;
&nbsp;&nbsp;&nbsp;&nbsp;}
};


template<typename T1, typename T2, typename Allocate>
class T2_base
{
public:
&nbsp;&nbsp;&nbsp;&nbsp;T2_base()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<<"print T1_base(templeate)"<<endl;
&nbsp;&nbsp;&nbsp;&nbsp;}
};


template<typename T1, typename T2, typename Allocate>
class T1_base<T1(T2), Allocate>
&nbsp;&nbsp;&nbsp;&nbsp;:public T2_base<T1, T2, allocate>
{
public:
&nbsp;&nbsp;&nbsp;&nbsp;T1_base()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<<"print T1_base(T2)"<<endl;
&nbsp;&nbsp;&nbsp;&nbsp;}
};

int add(int)
{
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;T1_base<int(int) ,int, int> base;

&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}



上面的偏特化报错如下:
e:\source_work\1.0 work\work\200801\200801\200801.cpp(33) : error C2976: “T1_base”: 模板 参数太少
1>        e:\source_work\1.0 work\work\200801\200801\200801.cpp(11) : 参见“T1_base”的声明
1>e:\source_work\1.0 work\work\200801\200801\200801.cpp(40) : error C2953: “T1_base”: 类模板已经定义
1>        e:\source_work\1.0 work\work\200801\200801\200801.cpp(11) : 参见“T1_base”的声明
1>e:\source_work\1.0 work\work\200801\200801\200801.cpp(50) : error C2133: “base”: 未知的大小
1>e:\source_work\1.0 work\work\200801\200801\200801.cpp(50) : error C2512: “T1_base”: 没有合适的默认构造函数可用


修改如下,则可以:
template<typename T1, typename T2, typename Allocate>
class T1_base<T1(T2), T2, Allocate>
&nbsp;&nbsp;&nbsp;&nbsp;:public T2_base<T1, T2, allocate>
{
public:
&nbsp;&nbsp;&nbsp;&nbsp;T1_base()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<<"print T1_base(T2)"<<endl;
&nbsp;&nbsp;&nbsp;&nbsp;}
};


既然事实如此,为什么作者在写这篇文章是举例:
template<typename R,typename T0,typename Allocator>
class function<R(T0),Allocator> //对R(T0)函数类型的偏特化版本


:public function1<R,T0,Allocator> //为R(T0)形式的函数准备的基类,在下面讨论

{
 typedef function1<R,T0,Allocator> base_type;
 typedef function selftype;
 struct clear_type{}; //马上你会看到这个蹊跷的类型定义的作用

 public:
  function() : base_type() {} //默认构造

  template<typename Functor> //模板化的构造函数,为了能够接受形式兼容的仿函数对象

  function(Functor f, typename enable_if<
    (ice_not<(is_same<Functor, int>::value)::value),
    int
  >::type = 0) :base_type(f){}

 function(clear_type*) : base_type() {} //这个构造函数的作用在下面解释

 self_type& operator=(const self_type& f) //同类型function对象之间应该能够赋值

 {
  self_type(f).swap(*this); //swap技巧,细节见《Effective STL》

  return *this;
 }
 ...
};

应该是错误的。
请大家指点啊。谢谢。

[ 本帖最后由 guhan010 于 2009-6-25 23:06 编辑 ]

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
2 [报告]
发表于 2009-06-26 00:45 |只看该作者

回复 #1 guhan010 的帖子

template<typename T1, typename T2, typename Allocate>
class T1_base;

这个是主模板,它有3个形式类型参数。

所以对该主模板的偏特化也必须有3个实际类型参数。


所以:
template<typename T1, typename T2, typename Allocate>
class T1_base<T1(T2), Allocate>;

就少了一个。


template<typename T1, typename T2, typename Allocate>
class T1_base<T1(T2), T2, Allocate>;
就可以, 第1个实际参数是T1和T2构成的类型T1(T2), 第2个是T2,第3个是Allocate。




template<typename Signature, //函数类型
typename Allocator = ...
> //Allocator并非重点,故不作介绍

class function;

这是主模板,有2个形式类型参数。

template<typename R,typename T0,typename Allocator>
class function<R(T0),Allocator> //对R(T0)函数类型的偏特化版本
主模板的偏特化, 有2个实际参数, 第1个是由R和T0构成的一个类型:R(T0),第2个是Allocator。

论坛徽章:
0
3 [报告]
发表于 2009-06-26 08:29 |只看该作者
楼上的意思是偏特化的类可以增加自己的模版数量?
如果是这样,那么问题迎刃而解,由于看了function大源码,发现都是有宏来替换的,没有见到function的模版类。只有特化类。
谢谢了,我试试。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP