473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Define functor within method?

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
2 2011
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CoolPint | last post by:
I have implemented a generic priority queue below and tested it works fine, but I have one small problem I cannot understand. I have type parameter F which determines the priority so that users can...
8
by: Pete C | last post by:
In this section of the FAQ: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10 an abstract destructor is given a definition with the cryptic comment "It's faster this...
9
by: RalphTheExpert | last post by:
Under MC++, what is the right way to define a (typedef'd) pointer to a static function? When I call a function through a pointer I get a stack overflow. For those having the same problem, I...
12
by: aaragon | last post by:
Hi everyone, I'm trying to provide some external functionality to a class through a functor object defined by the user. The concept is as follows: template <class Functor> class ClassA {...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
7
by: aaragon | last post by:
Hi everyone, I have a simple question. I'm trying to make a macro in one file so I can use it in main.cpp. The idea is that I the user of my code has simple to type the macro definition to replace...
1
by: Bit Byte | last post by:
I have the folowing functor declaration: template < class T, typename ReturnType, typename Parameter > class Functor : public NonCopyable { public: typedef ReturnType...
3
by: alan | last post by:
Hello all, I'd like to know if there is a nice method of defining a functor creator which accepts an N-ary function and returns a functor based on that function. For example I have a function:...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.