1.1 --- a/src/work/klao/tag_demo.cc Sun Apr 17 18:57:22 2005 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,95 +0,0 @@
1.4 -#include <iostream>
1.5 -#include <boost/utility.hpp>
1.6 -
1.7 -using namespace std;
1.8 -using namespace boost;
1.9 -
1.10 -struct True {
1.11 - static const bool value = true;
1.12 -};
1.13 -struct False {
1.14 - static const bool value = false;
1.15 -};
1.16 -
1.17 -// Here are some graph structures. Some of them define the type "OneTag"
1.18 -// to True or False, some does not. Not defining a tag is (or at least
1.19 -// should be) equivalent to defining it to "False".
1.20 -struct Graph1 {};
1.21 -struct Graph2 {
1.22 - typedef True OneTag;
1.23 -};
1.24 -struct Graph3 {
1.25 - typedef False OneTag;
1.26 -};
1.27 -
1.28 -
1.29 -/**************** The first method to use tags ****************/
1.30 -
1.31 -template <typename Graph, typename Enable = void>
1.32 -struct HasOneTag {
1.33 - typedef False TheTag;
1.34 -};
1.35 -
1.36 -// specialization for those graphs which defined the tag to be true:
1.37 -template <typename Graph>
1.38 -struct HasOneTag<Graph, typename enable_if<typename Graph::OneTag>::type > {
1.39 - typedef True TheTag;
1.40 -};
1.41 -
1.42 -template <typename Graph>
1.43 -int cn1(const Graph &, False) {
1.44 - return 0;
1.45 -}
1.46 -
1.47 -template <typename Graph>
1.48 -int cn1(const Graph &, True) {
1.49 - return 1;
1.50 -}
1.51 -
1.52 -template <typename Graph>
1.53 -int cn1(const Graph &g) {
1.54 - return cn1(g, typename HasOneTag<Graph>::TheTag());
1.55 -}
1.56 -
1.57 -/**************** The second method ****************/
1.58 -
1.59 -// An artificial type to provoke a conversion to avoid ambuguity...
1.60 -template <typename T>
1.61 -struct Wrap {
1.62 - const T &value;
1.63 - Wrap(const T &t) : value(t) {}
1.64 -};
1.65 -
1.66 -template <typename Graph>
1.67 -typename enable_if<typename Graph::OneTag, int>::type
1.68 -_cn2(const Graph &) {
1.69 - return 1;
1.70 -}
1.71 -
1.72 -template <typename Graph>
1.73 -int _cn2(Wrap<Graph>) {
1.74 - return 0;
1.75 -}
1.76 -
1.77 -template <typename Graph>
1.78 -int cn2(const Graph& g) {
1.79 - return _cn2<Graph>(g);
1.80 -}
1.81 -
1.82 -
1.83 -int main() {
1.84 - Graph1 g1;
1.85 - Graph2 g2;
1.86 - Graph3 g3;
1.87 -
1.88 - cout << "The first method:\n";
1.89 - cout << "G1: " << cn1(g1) << endl;
1.90 - cout << "G2: " << cn1(g2) << endl;
1.91 - cout << "G3: " << cn1(g3) << endl;
1.92 -
1.93 - cout << "The second method:\n";
1.94 - cout << "G1: " << cn2(g1) << endl;
1.95 - cout << "G2: " << cn2(g2) << endl;
1.96 - cout << "G3: " << cn2(g3) << endl;
1.97 -
1.98 -}