language: C++ 4.7.2 (gcc-4.7.2)
date: 590 days 9 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <cassert>
#include <iostream>
#include <string>
 
// helper for result type
 
    template <typename T, typename Next> struct Cons;
    struct ConsEmpty;
 
    template <typename Cons, typename U>
    struct cons_result;
 
    template <typename U>
    struct cons_result<ConsEmpty, U> {
      typedef Cons<U, ConsEmpty> type;
    };
 
    template <typename T, typename U>
    struct cons_result<Cons<T, ConsEmpty>, U> {
      typedef Cons<T, Cons<U, ConsEmpty> > type;
    };
 
    template <typename T, typename Next, typename U>
    struct cons_result<Cons<T, Next>, U> {
      typedef Cons<T, typename cons_result<Next, U>::type> type;
    };
 
    
// define cons
 
    template <typename T, typename Next>
    struct Cons {
      Cons(T t, Next n): value(t), next(n) {}
 
      T value;
      Next next;
 
      template <typename U>
      typename cons_result<Cons, U>::type operator()(U u) {
        typedef typename cons_result<Cons, U>::type Result;
        return Result(value, next(u));
      }
    };
 
    struct ConsEmpty {
      template <typename U>
      Cons<U, ConsEmpty> operator()(U u) { return Cons<U, ConsEmpty>(u, ConsEmpty()); }
    };
 
    template <typename T>
    Cons<T, ConsEmpty> cons(T t) { return Cons<T, ConsEmpty>(t, ConsEmpty()); }
 
 
//And here is how you could design `VarPrint` for it:
 
    bool VarPrint(std::ostream& out, const std::string& s, ConsEmpty) 
    {
        std::string::size_type offset = 0;
        if((offset = s.find("%")) != std::string::npos)
        {
            if(!(offset != s.size() - 1 && s[offset + 1] == '%'))
            {
                assert(!"Missing Arguments!");
                return false;
            }
        }
        out << s;
        return true;
    }
    
    template<typename T, typename Next>
    bool VarPrint(std::ostream& out,
                  std::string const& s,
                  Cons<T, Next> const& cons) 
    {
        std::string::size_type prev_offset = 0;
        std::string::size_type curr_offset = 0;
        while((curr_offset = s.find("%", prev_offset)) != std::string::npos)
        {
            out << s.substr(prev_offset, curr_offset);
            if(!(curr_offset != s.size() - 1 && s[curr_offset + 1] == '%'))
            {
                out << cons.value;
                if(curr_offset + 2 < s.length())
                    return VarPrint(out, s.substr(curr_offset + 2), cons.next);
                return true;
            }
    
            prev_offset = curr_offset + 2;
            if(prev_offset >= s.length())
                break;
        }
        assert(!"Extra Argument Provided!");
        return false;
    }
 
// and the demo
 
    int main() {
      VarPrint(std::cout, "integer %i\n", cons(1));
      VarPrint(std::cout, "mix of %i and %s\n", cons(2)("foo"));
    }