Boost logo

Boost :

Subject: Re: [boost] [local] Help for the Alternatives section
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2011-05-08 10:04:07


On Sun, May 8, 2011 at 5:02 AM, Mathias Gaunard
<mathias.gaunard_at_[hidden]> wrote:
> On 07/05/2011 18:00, Lorenzo Caminiti wrote:
>
>> Hello Mathias, I'm curious if you have an opinion about the usefulness
>> of Boost.Local supporting multiple types (but not generic types) for
>> its function parameters.
>
> Probably not worth the effort, it seems that it would be a lot of work.
>
> And you can still have overloading by doing it like this:
>
> void BOOST_LOCAL_FUNCTION(const double& num, const bind& sep)
>    std::cout << num << std::endl;
> BOOST_LOCAL_FUNCTION_NAME(l1)
>
> void BOOST_LOCAL_FUNCTION(const std::string& num, const bind& sep)
>    std::cout << num << std::endl;
> BOOST_LOCAL_FUNCTION_NAME(l2)
>
> auto /* or the real type */ l = overload(l1, l2);

Does Boost already have a functor overloader like overload() above?

If not, I can add something like overload<> below to Boost.Local:

#include <boost/local/function.hpp>
#include <boost/function.hpp>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

template<typename F0, typename F1, typename F2 = void, typename F3 = void>
struct overload {
};

template<typename F0R, typename F0A0, typename F1R, typename F1A0>
struct overload<F0R (F0A0), F1R (F1A0)> {
    overload(boost::function<F0R (F0A0)> f0, boost::function<F1R (F1A0)> f1):
            f0_(f0), f1_(f1) {}
    F0R operator()(F0A0 a0) const { return f0_(a0); }
    F1R operator()(F1A0 a0) const { return f1_(a0); }
private:
    boost::function<F0R (F0A0)> f0_;
    boost::function<F1R (F1A0)> f1_;
};

// More specializations to overload also with F2, F3, etc.

int main() {
    char sep = '\n';

    void BOOST_LOCAL_FUNCTION_PARAMS(const double& num, const bind& sep) {
        std::cout << num << sep;
    } BOOST_LOCAL_FUNCTION_NAME(print_d)

    void BOOST_LOCAL_FUNCTION_PARAMS(const std::string& num, const bind& sep) {
        std::cout << num << sep;
    } BOOST_LOCAL_FUNCTION_NAME(print_s)

    overload<void (const double&), void (const std::string&)> print(
            print_d, print_s);

    std::vector<double> d(2);
    d[0] = 1.2; d[1] = 3.4;
    std::for_each(d.begin(), d.end(), print);

    std::vector<std::string> s(3);
    s[0] = "ab"; s[1] = "cd"; s[2] = "ef";
    std::for_each(s.begin(), s.end(), print);

    return 0;
}

-- 
Lorenzo

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