Boost logo

Boost :

Subject: Re: [boost] [local] Help for the Alternatives section
From: lcaminiti (lorcaminiti_at_[hidden])
Date: 2011-05-12 12:05:57


Jeffrey Lee Hellrung, Jr.-2 wrote:
>
> On Fri, May 6, 2011 at 7:59 PM, Lorenzo Caminiti
> <lorcaminiti_at_[hidden]>wrote:
>
>> On Fri, May 6, 2011 at 2:07 PM, Lorenzo Caminiti
>> <lorcaminiti_at_[hidden]>
>> wrote:
>> > On Fri, May 6, 2011 at 1:32 PM, Jeffrey Lee Hellrung, Jr.
>> > <jeffrey.hellrung_at_[hidden]> wrote:
>> >> Would "const bind(int)& x" work?
>>
>> It think yes :) I should be able to support the syntax (also with
>> optional leading const and optional &):
>>
>> bind& x // deduced type (with Boost.Typeof)
>> bind(int)& x // specify type (no Boost.Typeof)
>>
>> More in general "[const] bind [(type)] [&] name" where "[t]" indicates
>> that token t is optional.
>>
> [...snip preprocessor details...]
>
> Awesome.
>

I have implemented this:

http://svn.boost.org/svn/boost/sandbox/local/libs/local/doc/html/boost_local/Advanced_Topics.html#boost_local.Advanced_Topics.specifying_bound_types

#include <boost/local/function.hpp>
#include <boost/local/block.hpp>
#include <boost/local/exit.hpp>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cassert>

class adder {
public:
    adder(): sum_(0.0) {}

    double sum(const std::vector<double>& nums, const int& factor = 10) {
        std::ostringstream out;

        void BOOST_LOCAL_FUNCTION_PARAMS(double num,
                const bind(const int&) factor, // Specify bind types
                bind(std::ostringstream)& out, // explicitly (so no
                bind(adder*) this) { // Boost.Typeof for binds).
            this_->sum_ += factor * num;
            out << "Summed: " << this_->sum_ << std::endl;
        } BOOST_LOCAL_FUNCTION_NAME(add)
        std::for_each(nums.begin(), nums.end(), add);

        BOOST_LOCAL_EXIT(const bind(std::ostringstream)& out) {
            std::cout << out.str();
        } BOOST_LOCAL_EXIT_END

        BOOST_LOCAL_BLOCK(bind(std::ostringstream)& out,
                const bind(adder*) this) {
            out << "Asserted: " << this_->sum_ << std::endl;
            assert(this_->sum_ > 0.0);
        } BOOST_LOCAL_BLOCK_END

        return sum_;
    }

private:
    double sum_;
};

int main() {
    std::vector<double> v(3);
    v[0] = 100.0; v[1] = 90.5; v[2] = 7.0;

    adder a;
    // sum = 10 * 100.0 + 10 * 90.5 + 10 * 7.0 = 1975.0
    assert(a.sum(v) == 1975.0);

    return 0;
}

--Lorenzo

--
View this message in context: http://boost.2283326.n4.nabble.com/local-Help-for-the-Alternatives-section-tp3408469p3517989.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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