On 3/12/2013 11:43 AM, Nathan Ridge wrote:
I've got a fusion set say:
set
s(...) I'd like to filter out empty optional<double> values when streaming out:
std::cout << s;
so for s(1, 2.0, true) ==> (1,2.0,true)
and for s(3, optional<double>(), false) ==> (2,false)
filter_if's predicate is an a Metafunction returning mpl::bool_ so it looks to me like this won't support run time?
Is there another facility that would achieve the above?
Perhaps you can use a polymorphic function object like:
struct output_if_not_none { output_if_not_none(ostream& os) : os_(os) {}
ostream& os_;
template <typename T> void operator()(const T& t) { os_ << t; }
template <typename T> void operator()(const optional<T>& t) { if (t) os_ << *t; } };
for_each(s, output_if_not_none(cout));
It doesn't do the formatting that you desire but it might be a starting point.
The problem with trying to do it with the syntax
cout << some_function(s);
is that the expression some_function(s) needs to have a type determined at compile time.
Yep that's what I'm running into. some_function will need to return a proxy that needs to grab the manipulator values tuple_open, *_close and *_delimiter and use the output_if_not_none function you show above to deal with leading, internal and trailing cases. Thanks, Jeff