Why C++ committee is so rigidity? They define lambda as exactly the
academism, and intentional didn't add a lambda self reference.
The best solution I have found is to bind the lambda to a
std::function<...> object.
The code snippet which need recursive lambda:
class Automata {
// more....
void print_output() const { // use recursive lambda
std::string str;
std::function<void(state_id_t,state_id_t,char_t)> lambda =
[&](state_id_t source,state_id_t target,char_t c) {
str.push_back(c);
if (is_term(target))
cout <<str << '\n';
for_each_move(target, lambda);
str.resize(str.size()-1); // pop_back
};
for_each_move(initial_state, lambda);
}
// more....
};
This code print a Tree Shape Automata by DFS order. The Automata is
careful designed for memory & speed, the function of for_each_move
complex but fast, so it is a template member function to iterate over
each direct target of specified state:
template<class OP> // use ctz (count trailing zero)
void for_each_move(state_id_t curr, OP op) const;
If there are some mechanism of lambda self refence, this code should
be more efficient and concise ( [[]] is the assumed lambda self
reference):
void print_output() const { // use recursive lambda
std::string str;
for_each_move(initial_state,
[&](state_id_t source,state_id_t target,char_t c) {
str.push_back(c);
if (is_term(target))
cout <<str << '\n';
for_each_move(target, [[]]);
str.resize(str.size()-1); // pop_back
});
}
--
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]