Define functor within method?

J

JH Trauntvein

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::string> my_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
 
M

mlimber

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::string> my_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.

It is not: a template argument may not reference a local type. Your
test also fails on VC 7.1 and 6.0, Comeau, and EDG.

For Lambda functions, you may want to look into Boost.Lambda (http://
boost.org/doc/html/lambda.html). _Beyond the C++ Standard Library_ by
Karlsson helps explain it.

Cheers! --M
 
P

Piyo

mlimber said:
It is not: a template argument may not reference a local type. Your
test also fails on VC 7.1 and 6.0, Comeau, and EDG.

For Lambda functions, you may want to look into Boost.Lambda (http://
boost.org/doc/html/lambda.html). _Beyond the C++ Standard Library_ by
Karlsson helps explain it.

Cheers! --M

Yeah it is so cool that I had to write it for you :)


--------------------------------------------------------
#include <list>
#include <algorithm>
#include <iostream>
#include <boost/lambda/lambda.hpp>

class MyClass
{
public:
typedef std::list<std::string> my_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"; }
// };
using namespace std;
using namespace boost::lambda;
for_each(my_strings.begin(),my_strings.end(), cout << _1 << "\n");
}

int
main()
{
MyClass foo;
foo.my_strings.push_back( "Hello" );
foo.my_strings.push_back( "World" );
foo.my_method();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top