| |||
| 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 !!! |
| |||
| >>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. |
![]() |
| 书签 |
| 主题工具 | |
| 显示模式 | |
| |
相似的主题 | ||||
| 主题 | 主题作者 | 版面 | 回复 | 最后发表 |
| 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 |