If I have a symbol table:
struct MySymbols : symbols<char, MyEnum::Fruits>
{
MySymbols ()
: symbols<char, MyEnum::Fruits>(std::string("MySymbols"))
{
add("apple", MyEnum::Apple)
("orange", MyEnum::Orange);
}
};
I want to iterate over the table in order to search for a symbol by data value. I cannot use lambda expressions so I implemented a simple class:
template<typename T>
struct SymbolSearcher
{
SymbolSearcher::SymbolSearcher(T searchFor)
: _sought(searchFor)
{
// do nothing
}
void operator() (std::basic_string<char> s, T ct)
{
if (_sought == ct)
{
_found = s;
}
}
std::string found() const { return _found; }
private:
T _sought;
std::string _found;
};
And I'm using it as follows:
SymbolSearcher<MyEnum::Fruits> search(ct);
MySymbols symbols;
symbols.for_each(search);
std::string symbolStr = search.found();
If I set a breakpoint on _found = s
I can confirm that _found is getting set, however search.found() always returns an empty string. I'm guessing it has something to do with the way the functor is being called inside for_each but I don't know.
What am I doing wrong?