blank

blank
Last edited on
You could try and implement std::optional yourself.
An extremely simple demo.
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
#include <iostream>

template<class T>
class Optional
{
public:
  explicit Optional(T value): mValue(value), mHasValue(true)
  {
  }
  bool hasValue() const noexcept
  {
    return mValue;    
  }
  T getValue() noexcept
  {
      return mValue;
  }
private:
  T mValue; 
  bool mHasValue = false;
};
int main()
{
    Optional<int> oi(10);
    if(oi.hasValue())
      std::cout << "Value of oi: " << oi.getValue();
    
}

On L71 you need to replace the structural binding with std::pair
If you can clarify what else to add from the demo that you've given to me? Its clear that there is more to it than just running it as it is and I'm still confused as to what to initialize, what must be considered and included, and I have no experience in implementing template classes in a while as far as I know. I'm still trying to find workarounds of the original code as it is, I'll keep your suggestion as noted.
Here is a direct backport from C++17 to C++11.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <deque>
#include <iostream>
#include <tuple>
#include <unordered_set>
#include <vector>

struct State;

struct Transition {
  char symbol;
  State *successor;
};

struct State {
  std::vector<State *> epsilon_transitions;
  std::vector<Transition> transitions;
  std::unordered_set<std::string> visited_strings;
};

struct Automaton {
  State *initial_state;
  State *final_state;
};

Automaton Eps() {
  Automaton c;
  c.initial_state = c.final_state = new State;
  return c;
}

Automaton Sym(char symbol) {
  Automaton c;
  c.initial_state = new State;
  c.final_state = new State;
  c.initial_state->transitions.push_back({symbol, c.final_state});
  return c;
}

Automaton Star(Automaton a) {
  Automaton c;
  c.initial_state = c.final_state = new State;
  c.final_state->epsilon_transitions.push_back(a.initial_state);
  a.final_state->epsilon_transitions.push_back(c.final_state);
  return c;
}

Automaton Cat(Automaton a, Automaton b) {
  Automaton c;
  c.initial_state = a.initial_state;
  c.final_state = b.final_state;
  a.final_state->epsilon_transitions.push_back(b.initial_state);
  return c;
}

Automaton Alt(Automaton a, Automaton b) {
  Automaton c;
  c.initial_state = new State;
  c.final_state = new State;
  c.initial_state->epsilon_transitions.push_back(a.initial_state);
  c.initial_state->epsilon_transitions.push_back(b.initial_state);
  a.final_state->epsilon_transitions.push_back(c.final_state);
  b.final_state->epsilon_transitions.push_back(c.final_state);
  return c;
}

void Enumerate(Automaton a, std::size_t size_limit = 4) // Kleene-Star operator, manages limited size to avoid infinity
{
  std::deque<std::pair<State *, std::string>> queue = {
      {a.initial_state, std::string()}};
  do {
    auto state = queue.front().first;
    auto string = queue.front().second;
    if (string.size() > size_limit) {
      std::cout << "...\n";
      break;
    }
    queue.pop_front();
    if (!state->visited_strings.insert(string).second) {
      continue;
    }
    if (state == a.final_state) {
      std::cout << "\"" << string << "\"\n";
    }
    for (State *successor : state->epsilon_transitions) {
      queue.push_front({successor, string});
    }
    for (auto transition : state->transitions) {
      auto symbol = transition.symbol;
      auto successor = transition.successor;
      queue.push_back({successor, string + std::string(1, symbol)});
    }
  } while (!queue.empty());
  std::cout << "\n";
}

#include <cctype>
#include <cstdio>
#include <istream>
#include <sstream>

char Peek(std::istream &input) {
  input >> std::ws;
  return input.peek();
}

bool Match(std::istream &input, char c) {
  if (Peek(input) == c) {
    input.get();
    return true;
  }
  return false;
}

bool ParseSymbol(std::istream &input, char& result) {
  if (std::isalpha(Peek(input))) {
    result = input.get();
    return true;
  }
  return false;
}

bool Parse(std::istream &input, Automaton& result);

bool ParsePrimitive(std::istream &input, Automaton& result) {
  char symbol;
  if (ParseSymbol(input, symbol)) {
    result = Sym(symbol);
    return true;
  }
  if (Match(input, '(')) {
    Automaton a;
    if (Parse(input, a)) {
      if (Match(input, ')')) {
        result = a;
        return true;
      }
    }
  }
  return false;
}

bool ParseFactor(std::istream &input, Automaton& result) {
  Automaton a;
  if (ParsePrimitive(input, a)) {
    if (Match(input, '*')) {
      result = Star(a);
      return true;
    }
    result = a;
    return true;
  }
  return false;
}

bool ParseTerm(std::istream &input, Automaton& result) {
  Automaton a;
  if (ParseFactor(input, a)) {
    while (Match(input, '.')) {
      Automaton b;
      if (ParseFactor(input, b)) {
        a = Cat(a, b);
      } else {
        return false;
      }
    }
    result = a;
    return true;
  }
  return false;
}

bool Parse(std::istream &input, Automaton& result) {
  Automaton a;
  if (ParseTerm(input, a)) {
    while (Match(input, '|')) {
      Automaton b;
      if (ParseTerm(input, b)) {
        a = Alt(a, b);
      } else {
        return false;
      }
    }
    result = a;
    return true;
  }
  return false;
}

bool Parse(const std::string &input, Automaton& result) {
  std::stringstream stream(input);
  Automaton a;
  if (Parse(stream, a)) {
    if (Peek(stream) == EOF) {
      result = a;
      return true;
    }
  }
  return false;
}

int main() {
  // Parse parameters won't allow number chars, how so?
  Automaton result;
  if (Parse("a", result)) { Enumerate(result); } else std::cout << "Failed to parse\n";
  if (Parse("a|b", result)) { Enumerate(result); } else std::cout << "Failed to parse\n";
  if (Parse("a.b", result)) { Enumerate(result); } else std::cout << "Failed to parse\n";
  if (Parse("(a|b).c", result)) { Enumerate(result); } else std::cout << "Failed to parse\n";
  if (Parse("(c|d).(a|b)", result)) { Enumerate(result); } else std::cout << "Failed to parse\n";
  if (Parse("a*", result)) { Enumerate(result); } else std::cout << "Failed to parse\n";
  if (Parse("(a|b)*", result)) { Enumerate(result); } else std::cout << "Failed to parse\n";
}
Your're genuinely a life-savior @mbozzi, thank you, your credentials are in place.
Topic archived. No new replies allowed.