473,394 Members | 1,696 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,394 software developers and data experts.

lambda

template < typename T >
typename T::const_iterator first_pipe(T const& cont)
{
namespace l = boost::lambda;
typedef esi::metafunc::dereference_type<typename T::value_type>::type
value_type; // value type is mock_pipe_base in this case.

return std::find_if(cont.begin(), cont.end(),
l::bind(&value_type::GetType, *(l::_1)) == DT_PIPE);
}

struct mock_pipe_base
{
bool pipe;
mock_pipe_base(bool b) : pipe(b) {}
DT_TYPE GetType() const {
return pipe ? DT_PIPE : DT_PUMP;
}
virtual void f() = 0;
};

struct mock_pipe : mock_pipe_base
{
mock_pipe(bool b) : mock_pipe_base(b) {}
void f() {}
};

typedef std::list<mock_pipe_base*list1_t;
list1_t l;
l.push_back(list1_t::value_type(new mock_pipe(false)));
l.push_back(list1_t::value_type(new mock_pipe(false)));
l.push_back(list1_t::value_type(new mock_pipe(false)));

list1_t::const_iterator fit = first_pipe(l);
This results in an attempt to instantiate a mock_pipe_base object,
which is of course not possible.

I can provide the real error if desired but it is quite lengthy. It
definately has something to do with the lambda expression. I'm hoping
someone will be able to look at that and know what I'm doing wrong and
be able to provide a solution. I'm about to disallow use of lambda in
our projects but I would like to come up with a solution and leave it
open with some guidelines for use.

Thanks.

Nov 29 '06 #1
4 1374
I've created the problem in slightly simpler code:

#include <iostream>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

struct test_b
{
test_b() { std::cerr << "test_b::test_b()\n"; }
test_b(test_b const&) { std::cerr << "test_b::test_b(test_b
const&)\n"; }
virtual int f() = 0; //{ return 5; }

};

struct test_d : test_b
{
test_d() : test_b() { std::cerr << "test_d::test_d()\n"; }
test_d(test_d const& d) : test_b(d) { std::cerr <<
"test_d::test_d(test_d const&)\n"; }
int f() { return 5; }
};

int main(void)
{
namespace l = boost::lambda;

test_b & td = test_d();

std::cerr << "Value: " <<
(l::bind(&test_b::f, l::_1))(td) << "\n";

int x; std::cin >x;
}
Same error message. I'm beginning to think that lambda simply can't
handle polymorphic types at all....which makes it almost 100% useless.

Nov 29 '06 #2
Noah Roberts wrote:
Same error message. I'm beginning to think that lambda simply can't
handle polymorphic types at all....which makes it almost 100% useless.
This may not help your particular situation, but changing from
references to pointers seems to work...

//test_b & td = test_d();
test_b *td = new test_d();

Greg Buchholz

Nov 30 '06 #3

Greg Buchholz wrote:
Noah Roberts wrote:
Same error message. I'm beginning to think that lambda simply can't
handle polymorphic types at all....which makes it almost 100% useless.

This may not help your particular situation, but changing from
references to pointers seems to work...

//test_b & td = test_d();
test_b *td = new test_d();
Yeah, that doesn't help as it doesn't allow me to work with smart
pointers.

Nov 30 '06 #4
Noah Roberts wrote:
I've created the problem in slightly simpler code:

#include <iostream>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

struct test_b
{
test_b() { std::cerr << "test_b::test_b()\n"; }
test_b(test_b const&) { std::cerr << "test_b::test_b(test_b
const&)\n"; }
virtual int f() = 0; //{ return 5; }

};

struct test_d : test_b
{
test_d() : test_b() { std::cerr << "test_d::test_d()\n"; }
test_d(test_d const& d) : test_b(d) { std::cerr <<
"test_d::test_d(test_d const&)\n"; }
int f() { return 5; }
};

int main(void)
{
namespace l = boost::lambda;

test_b & td = test_d();
Do you really want to bind a temporary to a non-const reference? My compiler
complained.
std::cerr << "Value: " <<
(l::bind(&test_b::f, l::_1))(td) << "\n";

int x; std::cin >x;
}
Same error message. I'm beginning to think that lambda simply can't
handle polymorphic types at all....which makes it almost 100% useless.
I think the problem is that you pass td by value. At that point type
information is lost. Maybe using ref() helps, e.g.:

#include <iostream>
#include <tr1/functional>

struct test_b
{
test_b() { std::cerr << "test_b::test_b()\n"; }
test_b(test_b const&)
{ std::cerr << "test_b::test_b(test_b const&)\n"; }
virtual int f() = 0; //{ return 5; }

virtual ~test_b () {}
};

struct test_d : test_b
{
test_d() : test_b()
{ std::cerr << "test_d::test_d()\n"; }
test_d(test_d const& d) : test_b(d)
{ std::cerr << "test_d::test_d(test_d const&)\n"; }
int f() { return 5; }
};

int main(void)
{
test_d td;
test_b & tb = td;
std::cerr << "Value: " <<
(std::tr1::bind(&test_b::f, std::tr1::ref(tb)))() << "\n";
}
Best

Kai-Uwe Bux
Nov 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
25
by: Russell | last post by:
I want my code to be Python 3000 compliant, and hear that lambda is being eliminated. The problem is that I want to partially bind an existing function with a value "foo" that isn't known until...
4
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
1
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.