By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

Define functor within method?

P: n/a
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

Mar 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mar 15, 8:23 am, "JH Trauntvein" <j.trauntv...@comcast.netwrote:
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.
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

Mar 15 '07 #2

P: n/a
mlimber wrote:
On Mar 15, 8:23 am, "JH Trauntvein" <j.trauntv...@comcast.netwrote:
>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.

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::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"; }
// };
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();
}
Mar 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.