Boost logo

Boost :

Subject: Re: [boost] [local_function] any interest in a LocalFunction library?
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-09-16 01:07:33


On Mon, Sep 13, 2010 at 5:33 PM, Pierre Morcello
<pmorcell-cppfrance_at_[hidden]> wrote:
> Concerning the 'default' values that you proposed, it is an interesting idea, though I am not sure I would have a use for it.

Here's an example of how default parameters might be useful (together
with recursion):

#include <contract/detail/local_function.hpp>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>

int main () {
    std::vector<int> v;
    v.push_back(1);
    v.push_back(4);
    v.push_back(7);
    std::ostringstream factorials;

    CONTRACT_DETAIL_LOCAL_FUNCTION(
    (int) (factorial)(
            (int)(n)
            (bool)(recursion)(default)(false)
            (bound)((&factorials))
        )
    ) {
        int result = 0;

        if (n < 2 ) result = 1;
        else result = n * factorial(n - 1, true); // Recursive call.

        if (!recursion) factorials << result << " ";
        return result;
    } CONTRACT_DETAIL_LOCAL_FUNCTION_END(factorial)

    std::for_each(v.begin(), v.end(), factorial);
    std::cout << factorials.str() << std::endl;

    return 0;
}

However, in general, default parameters are used to overload a
function API. Therefore, I think they make much more sense when used
in the public API as supposed in local functions which are in nature
part of the implementation.

(BTW, I just got the code above to work. It required quite a bit of
re-work to get the template parameter trick to function together with
default parameters...)

--
Lorenzo

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