473,326 Members | 2,012 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,326 software developers and data experts.

boost::lambda

Hi,

After reading a bit about boost::lambda, I became curious how they
implemented it. I downloaded it and had a look, but the all the
headers and multiple templates make it a bit difficult to follow in a
short time (I only spent maybe 15 minutes).

Anyway, I decided to try some ideas out and came up with the following
basic example: (obviously this is very far from boost::lambda and is
special for streams, etc., but just trying to understand the basic
techniques they use).

template<class T>
class Placeholder
{
public:
Placeholder() { }

void operator()(std::string x)
{
*stream << x;
}

std::ostream *stream;
};
Placeholder<std::string> X;

template<class T>
Placeholder<T>& operator<<(std::ostream& os, Placeholder<T>& t) {
t.stream = &os;
return t;
}
int main(int argc, char **argv)
{
(std::cout << X)("test"); // outputs "test"
}
Is this the *basic* idea of how they accomplish this?

One question I have that I couldnt figure out is, since the placeholder
needs to store a value, how can this be done without knowing before
hand? In my example, I delcared X to be Placeholder<std::string> since
I am using a string in my simple test, but I know with boost::lambda, I
could put other types and _1 just works. I guess it is some template
magic that I can't figure out right now, but any hints / tips
appreciated.
Thanks in advance.

Jun 28 '06 #1
1 2676
flopbucket wrote:
After reading a bit about boost::lambda, I became curious how they
implemented it. I downloaded it and had a look, but the all the
headers and multiple templates make it a bit difficult to follow in a
short time (I only spent maybe 15 minutes).

Anyway, I decided to try some ideas out and came up with the following
basic example: (obviously this is very far from boost::lambda and is
special for streams, etc., but just trying to understand the basic
techniques they use).

(snip code)

Is this the *basic* idea of how they accomplish this?

One question I have that I couldnt figure out is, since the placeholder
needs to store a value, how can this be done without knowing before
hand? In my example, I delcared X to be Placeholder<std::stringsince
I am using a string in my simple test, but I know with boost::lambda, I
could put other types and _1 just works. I guess it is some template
magic that I can't figure out right now, but any hints / tips
appreciated.
Here is my attempt. This is the smallest example I could throw
together that (a) compiles, and (b) does something interesting.

namespace {
// This is basically the placeholder
class identity_functor {
public:
// Return type
template<typename arg1_type>
struct return_type {
typedef arg1_type type;
};
// Constructor (use default)
// Operator
template<typename arg1_type>
typename return_type<arg1_type>::type operator()(arg1_type s) {
return s;
}
};

// Constant functor
template<typename T>
class const_functor {
T _data;
public:
// Return type
template<typename arg1_type>
struct return_type {
typedef T type;
};
// Constructor (implicit)
const_functor(T __data)
: _data(__data) { }
// Operator
template<typename arg1_type>
typename return_type<arg1_type>::type operator()(arg1_type) {
return _data;
}
};

// Helper function (I shouldn't need this)
template <typename T>
const_functor<Tconstant(T t) {
return const_functor<T>(t);
}

// Sum functor
template <typename left_type,typename right_type>
class sum_functor {
left_type _left;
right_type _right;
public:
// Return type
template <typename arg1_type>
struct return_type {
typedef typename left_type::template
return_type<arg1_type>::type type;
};
// Constructor
sum_functor(left_type __left,right_type __right)
: _left(__left),
_right(__right) { }
// Operator
template<typename arg1_type>
typename return_type<arg1_type>::type operator()(arg1_type s) {
return _left(s)+_right(s);
}
};

// + operator
template<typename left_type,typename right_type>
sum_functor<left_type,right_typeoperator+(left_typ e
_left,right_type _right) {
return sum_functor<left_type,right_type>(_left,_right);
}

// Placeholders
identity_functor X;
}

int main() {
std::cout << (X+constant(1))(5) << std::endl;
return 0;
}

It appears to me that this is basically how boost::lambda works. The
trickiest part seems to be determining the return types. I just chose
to use the left-hand argument of addition to determine the type,
although it would be better to make a two-argument template which
picked the more precise type of the two. We just pass around arg1_type
as a template argument which is the type of the placeholder.

Several caveats: First, I had to put it into an unnamed namespace
because I use global templates to overload the operator. I wish I
could have a lambda_functor base class, but we can't have the templated
operator() be virtual (and we probably don't want to use inheritance
anyway, since it would slow things down). One downside to this is the
explicit constant() call which is required so that we operate on two
functor objects.

Second, there's a lot of issues with type traits. The reason I chose
operator+ as an example is because streams require references, while
these are all values. To get any more complicated, we'd need to have
to get into casting the template types into references and consts, and
I really don't understand that at all.

If anyone has further comments, I would really appreciate your input.
--
Steve Hicks

Jul 3 '06 #2

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

Similar topics

3
by: Rafal Dabrowa | last post by:
Suppose I have the following structure: struct xy { int x, y; }; and the following vector: vector<xy> v(10); I want to use boost/lambda for find an item in the table. Let's say, with x...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
5
by: marius lazer | last post by:
I have an STL container of functors and I want to execute them using std::for_each. In the code snippet below the line marked "// good" works fine and the one marked "// nothing" compiles fine but...
4
by: silverburgh.meryl | last post by:
I have code which uses Boost lambda in a template like this: using namespace boost::lambda; template<class T> bool lessThanXY( T& src, T& dest ) { return (src.getY() < dest.getY()); } ...
3
by: rammel | last post by:
hi, can't I call member functions from an placeholder of a boost-like lambda expression? look on the example below please: -----------------------------------------------------------...
0
by: Barry Ding | last post by:
#include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> using namespace std; using namespace boost::lambda; struct A { A(int i) : i(i) {} void print() const { cout <<...
1
by: nandor.sieben | last post by:
Let m be of type vector<vector<int. I am trying to sort each element of m: for_each(m.begin(), m.end(), bind(sort, *_1.begin(), *_1.end()); This does not work: 'const struct...
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...
3
by: Stuart Golodetz | last post by:
Hi, Just wondering why the following code doesn't work: #include <iostream> #include <boost/lambda/lambda.hpp> using namespace boost::lambda; int main()
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.