Boost logo

Boost :

Subject: Re: [boost] [range] [extensions] Default values for range access adaptors?
From: Akira Takahashi (faithandbrave_at_[hidden])
Date: 2013-06-06 04:17:39


2013/6/6 Akira Takahashi <faithandbrave_at_[hidden]>

> Hi Michel,
> ...
> I see. `optional_front` usage is following:
>
> if (optional<T> x = r | optional_front()) {
> std::cout << x.get() << std::endl;
> }
>
> If `front` return signle range, the usage is following:
>
> for (T x : x | front) {
> std::cout << x << std::endl;
> }
>
> If `front` is error, the return value is empty range.
> I think single range approach is better. However, I think now `front` and
> `optional_front` is useful.
>
> I would like to add `optional_front` and `front_range`.
>

I implemented `optional_front`, `optional_back`, and `optional_at`.
https://github.com/faithandbrave/OvenToBoost/commit/3c828848be96de1c7d5d17a9ea068596035321bf

usage:

#include <iostream>
#include <vector>
#include <boost/assign/list_of.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/access/front.hpp>

bool is_even(int x) { return x % 2 == 0; }

int main()
{
    using namespace boost::adaptors;
    using namespace boost::range::access;

    std::vector<int> v = boost::assign::list_of(1)(2)(3);

    if (boost::optional<int&> x = v | filtered(is_even) | optional_front) {
        std::cout << x.get() << std::endl;
    }
    else {
        std::cout << "not found" << std::endl;
    }
}

result:
2

Thanks,
Akira

>>========================
Akira Takahashi
mailto:faithandbrave_at_[hidden]
site : https://sites.google.com/site/faithandbrave/about/en


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk