-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathgoal.cpp
40 lines (33 loc) · 813 Bytes
/
goal.cpp
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
/*
* C++ solution using a functor & template metaprogramming
*
* build and run with e.g.
* g++ -std=c++11 -Wall -pedantic -Wextra -o goal goal.cpp && ./goal
*/
#include <iostream>
#include <string>
template<unsigned N=0>
struct repeater {
repeater(const std::string& init)
: init_(init) { }
std::string operator()(const std::string& terminal) const
{
std::string res = init_;
for (unsigned n = 0; n < N; ++n)
res += "o";
res += terminal;
return res;
}
repeater<N+1> operator()() const
{
return repeater<N+1>(init_);
}
const std::string init_;
};
const auto g = repeater<>("g");
int main()
{
std::cout << g("al") << '\n';
std::cout << g()("al") << '\n';
std::cout << g()()()()()()("al") << '\n';
}