返回   cpper编程论坛 > C/CPP/TMP/GP
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2008-02-09
初级会员
 
注册日期: 2006-11-18
住址: chn
帖子: 8
mjus 正向着好的方向发展
默认 Marco or trait or something magic ?

Hi ! Happy New Year to All !

//Given a POD struct,
struct recordType {
int id; // act as a key field
char name[12];
// more fields if any
};

// prototype: DBT<recordType, keyType, numOfbuckets, sizeOfkeyType>
DBT<recordType, int, 117, sizeof(int)> db;
recordType input;

// fill in data (id, name) for input

// prototype: bool Insert(keyType, recordType), keyType is int for this case.

if(db.Insert(input.id, input)) // { keyType => recordType } ( mapping ! )
cout<<"OK\n";
else
cout<<"ERR\n";

// A programmer is always lazy, at least wants to save typing !
// A dirty solution:
// In coding, after the declaration of 'struct recordType', define a Marco, INSERT(x) for a shortcut.


#define INSERT(x) Insert(x.id,x)

// Then, we could write
if(db.INSERT(input)) // hide the key field, keyType
cout<<"OK\n";
else
cout<<"ERR\n";


So far, so good ! Right ? Thank you for reading this pseudocode.

Now, here is my question coming:
Can we eliminate the Marco above by using trait, or something magic (i.e.,
field extractions) tech. ? because I think the Marco is not flexible enough.
Any input makes a difference & is welcome !!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2008-02-18
liuxinyu 的头像
高级会员
 
注册日期: 2006-02-09
帖子: 303
文章: 48
liuxinyu 正向着好的方向发展
默认 回复: Marco or trait or something magic ?

引用:
作者: mjus 查看帖子
代码:
//INSERT(x) for a shortcut. #define INSERT(x) Insert(x.id,x)
just quickly went through, why this can't be defined as a function template for DB?

as:
代码:
//inside class definition template<typename T> bool insert(T x){ return insert(x.id, x); }
if it is only applied to the POD record, the function template can be replaced by the specified recordType.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2008-02-21
初级会员
 
注册日期: 2006-11-18
住址: chn
帖子: 8
mjus 正向着好的方向发展
默认 回复: Marco or trait or something magic ?

>>why this can't be defined as a function template for DB?
>>//inside class definition
>>template<typename T>
>>bool insert(T x){ return insert(x.id, x); }


My point here is that the template class DB is provided with a generic type
, keyType and that the template class DB is served as a library. Therefore,

if the function template above is defined inside the class, because of its
hardcode, then the class DB can not be served as a library.

The recordType is limited to a POD type, that is the keyType must be one of
(int, long, double, and char*) only.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用


相似的主题
主题 主题作者 版面 回复 最后发表
C语言里有没有什么magic macro可以把一种类型不支持数组的变成支 yumagi C/CPP/TMP/GP 35 2005-04-17 01:57 PM
怎么实现这个trait? ajoo C/CPP/TMP/GP 33 2004-10-28 05:58 AM


所有时间均为格林尼治时间 +9。现在的时间是 07:14 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0