oven::constants

constantsは、範囲をconst Rangeに変換するRangeアダプタです。


C++0xで各コンテナに追加されるcbegin/cendのように
範囲を変更しないことを表明するために使用できます。

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/range.hpp>
#include <pstade/oven/initial_values.hpp>
#include <pstade/oven/constants.hpp>

using namespace pstade::oven;

template <class R, class F>
void for_each(R& r, F f)
{
    std::cout << "ref for_each" << std::endl;
    std::for_each(boost::begin(r), boost::end(r), f);
}

template <class R, class F>
void for_each(const R& r, F f)
{
    std::cout << "cref for_each" << std::endl;
    std::for_each(boost::begin(r), boost::end(r), f);
}

void disp(int x) { std::cout << x << std::endl; }

int main()
{
    std::vector<int> v = initial_values(1, 2, 3);

    for_each(v, &disp);
    for_each(v|constants, &disp);
}
ref for_each
1
2
3
cref for_each
1
2
3

【参照】
pstade::oven::constants