# HG changeset patch # User klao # Date 1100116754 0 # Node ID 04591f9a417306c12da37be7d7a9a9b747850440 # Parent 12b9993b217c7183760a6ff8e683d286b38d55ae A demonstration how to use _optional_ boolean tags. diff -r 12b9993b217c -r 04591f9a4173 src/work/klao/Makefile --- a/src/work/klao/Makefile Wed Nov 10 12:51:30 2004 +0000 +++ b/src/work/klao/Makefile Wed Nov 10 19:59:14 2004 +0000 @@ -1,4 +1,4 @@ -BINARIES = path_test map_test iter_map_test -INCLUDEDIRS= -I. -I.. -I../.. -I../{marci,jacint,alpar,johanna,athos,akos} +BINARIES = tag_demo +INCLUDEDIRS= -I. -I.. -I../.. -I../{marci,jacint,alpar,johanna,athos,akos} -I$(HOME)/boost include ../makefile diff -r 12b9993b217c -r 04591f9a4173 src/work/klao/tag_demo.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/klao/tag_demo.cc Wed Nov 10 19:59:14 2004 +0000 @@ -0,0 +1,95 @@ +#include +#include + +using namespace std; +using namespace boost; + +struct True { + static const bool value = true; +}; +struct False { + static const bool value = false; +}; + +// Here are some graph structures. Some of them define the type "OneTag" +// to True or False, some does not. Not defining a tag is (or at least +// should be) equivalent to defining it to "False". +struct Graph1 {}; +struct Graph2 { + typedef True OneTag; +}; +struct Graph3 { + typedef False OneTag; +}; + + +/**************** The first method to use tags ****************/ + +template +struct HasOneTag { + typedef False TheTag; +}; + +// specialization for those graphs which defined the tag to be true: +template +struct HasOneTag::type > { + typedef True TheTag; +}; + +template +int cn1(const Graph &, False) { + return 0; +} + +template +int cn1(const Graph &, True) { + return 1; +} + +template +int cn1(const Graph &g) { + return cn1(g, typename HasOneTag::TheTag()); +} + +/**************** The second method ****************/ + +// An artificial type to provoke a conversion to avoid ambuguity... +template +struct Wrap { + const T &value; + Wrap(const T &t) : value(t) {} +}; + +template +typename enable_if::type +_cn2(const Graph &) { + return 1; +} + +template +int _cn2(Wrap) { + return 0; +} + +template +int cn2(const Graph& g) { + return _cn2(g); +} + + +int main() { + Graph1 g1; + Graph2 g2; + Graph3 g3; + + cout << "The first method:\n"; + cout << "G1: " << cn1(g1) << endl; + cout << "G2: " << cn1(g2) << endl; + cout << "G3: " << cn1(g3) << endl; + + cout << "The second method:\n"; + cout << "G1: " << cn2(g1) << endl; + cout << "G2: " << cn2(g2) << endl; + cout << "G3: " << cn2(g3) << endl; + +}