Generic Programming (转)[@more@]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
#include
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
{
};
class Glassbin:public vector
{
};
template
struct Int2Type
{
enum{value=v};
};
template
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
Add(Trashs trash, Bin bin)
{
cout<::exists<_Add(trash,bin,Int2Type::exists >());
}
template
_Add(Trash trash, Bin bin,Int2Type)
{
bin.push_back(trash);
cout<};
template
_Add(Trash trash, Bin bin,Int2Type)
{
cout<};
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 e Mail at bu3bu4@263 .NET.
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
#include
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
{
};
class Glassbin:public vector
{
};
template
struct Int2Type
{
enum{value=v};
};
template
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
Add(Trashs trash, Bin bin)
{
cout<::exists<_Add(trash,bin,Int2Type::exists >());
}
template
_Add(Trash trash, Bin bin,Int2Type)
{
bin.push_back(trash);
cout<};
template
_Add(Trash trash, Bin bin,Int2Type)
{
cout<};
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 e Mail at bu3bu4@263 .NET.
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-979295/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10752019/viewspace-979295/