F
flopbucket
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:stream *stream;
};
Placeholder<std::string> X;
template<class T>
Placeholder<T>& operator<<(std:stream& 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.
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:stream *stream;
};
Placeholder<std::string> X;
template<class T>
Placeholder<T>& operator<<(std:stream& 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.