Generic Programming

原创 2002年01月18日 09:32:00
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.

《Generic Programming and the STL》读书笔记

Generic Programming and the STL   最后还需要把没搞懂的地方一一记下列出搞懂,争取一次把STL吃透,并在工作中学以致用!!别囫囵吞枣! 译序 作为一个广为人知的...
  • backard
  • backard
  • 2013年01月23日 20:07
  • 660

Java中的泛型编程(generic programming)和泛型类(generic class)

泛型编程(generic programming)和泛型类(generic class)泛型编程(generic programming)意味着代码可以被不同类型的对象所复用。泛型类(generic ...
  • lichengyu
  • lichengyu
  • 2015年12月06日 10:49
  • 1670

Generic Programming in C++

2.1 Introduction In generic programming, we take the notion of an ADT a step further. Instead of wr...
  • seamanj
  • seamanj
  • 2016年02月15日 23:48
  • 626

Hadoop系列之ToolRunner与GenericOptionsParser用法

首先给一个ToolRunner类的实例 package hadoop.study; /** * Created by denglinjie on 2017/3/7. */ import jav...
  • u011734144
  • u011734144
  • 2017年03月07日 17:37
  • 174

Generics in the Java Programming Language Java

Generics in the Java Programming LanguageJava编程语言中的泛型Gilad BrachaJuly 5, 2004Contents内容1 Introductio...
  • dracularking
  • dracularking
  • 2008年03月17日 22:12
  • 2307

GP与OOP

GP   :generic programming 类属编程 另一术语 泛型编程 (多么大气的一个词汇) OOP:Object Oriented Programming 面向对象编程 类属编程是构成...
  • sakawa_x
  • sakawa_x
  • 2014年12月28日 01:10
  • 662

iterator & Generic Programming demo

#include #include #include using namespace std; int main() { vector ivec { 1, 2, 100, 78, 8...
  • sentinel911
  • sentinel911
  • 2015年03月15日 20:11
  • 225

Generic Programming Techniques

Generic Programming Techniques This is an incomplete survey of some of the generic programming tech...
  • zhouchuan15
  • zhouchuan15
  • 2011年01月06日 11:27
  • 556

混沌 IN C++::Generic Programming

难度:  Normal 0 7.8 磅 0 2 false false false Micr...
  • Jinhao
  • Jinhao
  • 2008年10月03日 14:29
  • 1362

C#泛型编程(Generic Programming)

我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一个是处理string数据,或者其他自定义的数据类型,但我们没有办法,只能分别写多个方法处理每个数据类型,因为方法的参数类...
  • a769901686
  • a769901686
  • 2014年03月05日 16:53
  • 822
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Generic Programming
举报原因:
原因补充:

(最多只允许输入30个字)