[proto] vararg expressions to string

I am trying understand the use of proto::fold to handle vararg
expressions. As an experiment, I am trying to convert expressions of
the form foo(a,b,c) to a string of the form "foo(a,b,c)". I currently
have the following code where I treat the vararg expression as a
fusion sequence and convert each parameter to a string using
appropriate grammar. In the operator() of _foo_tostr, I have a case
for each length of the vararg expression. Please let me know if there
are better ways of doing this.
#include <iostream>
#include
{};
struct _foo_tostr
: proto::callable {
typedef string result_type;
template<typename T>
result_type operator()(T const &e) const {
stringstream sout;
sout << "foo(";
if(fusion::size(e) == 1) {
sout << ")";
}
else if(fusion::size(e) == 2) {
sout << var_tostr()(fusion::at_c<1>(e))
<< ")";
}
else if(fusion::size(e) == 3) {
sout << var_tostr()(fusion::at_c<1>(e)) << ", "
<< var_tostr()(fusion::at_c<2>(e))
<< ")";
}
else if(fusion::size(e) == 4) {
sout << var_tostr()(fusion::at_c<1>(e)) << ", "
<< var_tostr()(fusion::at_c<2>(e)) << ", "
<< var_tostr()(fusion::at_c<3>(e))
<< ")";
}
return sout.str();
}
};
struct foo_tostr
: proto::or_ <
proto::when

Sorry for the delay. I was away for a bit. On 2/18/2010 1:12 PM, Manjunath Kudlur wrote:
I am trying understand the use of proto::fold to handle vararg expressions. As an experiment, I am trying to convert expressions of the form foo(a,b,c) to a string of the form "foo(a,b,c)". I currently have the following code where I treat the vararg expression as a fusion sequence and convert each parameter to a string using appropriate grammar.
More or less OK so far. In the operator() of _foo_tostr, I have a case
for each length of the vararg expression. Please let me know if there are better ways of doing this.
<snip>
There's room for improvement here. Like you, I found it simpler to treat
the vararg as a Fusion sequence than trying to use proto::fold on the
expression. But your code can be made simpler with fusion::fold. See below.
#include <string>
#include <sstream>
#include <iostream>
#include
participants (2)
-
Eric Niebler
-
Manjunath Kudlur