Generic Programming

bu3bu4 2002-01-17 08:32:23
Hello all. It's nice to meet you all here. I got a lot of knowledge from csdn. now, I try to feed back some. the topic is about Generic Programming.

I paste a snippet to show how it works.
Everyone who has studied the book Think in C++ 2nd Edition knows the sample, trash collection, which is at the end of chapter 10, Design pattern.
Ok, let's begin with this.
the sample mentioned in Think in C++ using double dispatch to implement the design. and I use Generic Programming method to do the same thing.
Enjoy.

#include<vector>
#include <iostream>
using namespace std;
class Trash
{
};
class Paperbin;
class Glassbin;
class Paper:public Trash
{
public:
typedef Paperbin aBin;
};

class Glass:public Trash
{
public:
typedef Glassbin aBin;
};

class Paperbin:public vector<Paper>
{
};
class Glassbin:public vector<Glass>
{
};

template<int v>
struct Int2Type
{
enum{value=v};
};
template<class T, class U>
class Conversion
{
typedef char Small;
struct Big {char dummy[2];
};
static Small Test(U);
static Big Test(...);
static T MakeT();
public:
enum{ exists=(sizeof(Test(MakeT()))==sizeof(Small))};
};
template<class Trashs, class Bin>
Add(Trashs trash, Bin bin)
{
cout<<Conversion<Trashs::aBin ,Bin>::exists<<endl;
_Add(trash,bin,Int2Type<Conversion<Trashs::aBin ,Bin>::exists >());
}

template<class Trash, class Bin>
_Add(Trash trash, Bin bin,Int2Type<true>)
{
bin.push_back(trash);
cout<<"success adding"<<endl;
};
template<class Trash, class Bin>
_Add(Trash trash, Bin bin,Int2Type<false>)
{
cout<<"failed adding"<<endl;
};

int main()
{

Paper apaper;
Glass aglass;
Paperbin apaperbin;
Glassbin aglassbin;
Add(apaper,apaperbin);
Add(aglass,aglassbin);
Add(apaper,aglassbin);
Add(aglass,apaperbin);
return 0;
}

you can reach me by send me a email at bu3bu4@263.net.
...全文
83 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
upig 2002-01-18
  • 打赏
  • 举报
回复
不错
babysloth 2002-01-18
  • 打赏
  • 举报
回复
1、您这不是GP,而是利用了Loki库中的Conversion和Int2Type。
2、不要继承STL容器类。
qiu_weiping 2002-01-17
  • 打赏
  • 举报
回复
me too!!!!!!!!!!
jims8000 2002-01-17
  • 打赏
  • 举报
回复
I don't know what's you meaning
In Modern C++ Design , Andrei Alexandrescu opens new vistas for C++ programmers. Displaying extraordinary creativity and programming virtuosity, Alexandrescu offers a cutting-edge approach to design that unites design patterns, generic programming, and C++, enabling programmers to achieve expressive, flexible, and highly reusable code. This book introduces the concept of generic components—reusable design templates that produce boilerplate code for compiler consumption—all within C++. Generic components enable an easier and more seamless transition from design to application code, generate code that better expresses the original design intention, and support the reuse of design structures with minimal recoding. The author describes the specific C++ techniques and features that are used in building generic components and goes on to implement industrial strength generic components for real-world applications. Recurring issues that C++ developers face in their day-to-day activity are discussed in depth and implemented in a generic way. These include: Policy-based design for flexibility Partial template specialization Typelists—powerful type manipulation structures Patterns such as Visitor, Singleton, Command, and Factories Multi-method engines For each generic component, the book presents the fundamental problems and design options, and finally implements a generic solution. Paperback: 360 pages Publisher: Addison-Wesley Professional; 1 edition (February 23, 2001) Language: English ISBN-10: 0201704315 ISBN-13: 978-0201704310

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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