I recently tried an experiment to see if I could define a functor
within a class method as shown here:
#include <list>
#include <algorithm>
#include <iostream>
class MyClass
{
public:
typedef std::list<std::stringmy_strings_type;
my_strings_type my_strings;
void my_method();
};
void MyClass::my_method()
{
struct do_something
{
void operator ()(my_strings_type::value_type &str) const
{ std::cout << str << "\n"; }
};
std::for_each(my_strings.begin(),my_strings.end(), do_something());
}
The first compiler I used, vc8, allows this to compile. When I tried
compiling this against G++ yesterday, I got an undefined symbol
message and the name of the symbol appeared to include the enclosing
method name. I recognise that the code above is probably non-
conforming although I have no certainty. The thing is, if the functor
is only used within the scope of one method, I would really rather
define it there if possible as this more closely resembles lambda
functions and, in my opinion, makes the code more clear.
My question is about whether the code above is conforming.
Regards,
Jon Trauntvein