Boost logo

Boost Users :

Subject: Re: [Boost-users] [fusion] filter_if at run-time???
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2013-03-12 11:43:26


> I've got a fusion set say:
>
> set<int, optional<double>, bool> 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.

Regards,
Nate
                                               


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net