Boost logo

Boost Users :

From: Kirit Sælensminde (kirit.saelensminde_at_[hidden])
Date: 2007-09-29 01:31:35


I've been working on implementing some functional idioms in C++ in order
to help me learn the Boost.Bind, Boost.Lambda and Boost.Function libraries.

I have this (which uses Boost.Bind)

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/bind/placeholders.hpp>
#include <cmath>
#include <iostream>

template< typename R, typename V1, typename V2 >
boost::function< R ( V2 ) > papply( boost::function< R ( V1, V2 ) > f,
V1 v ) {
        return boost::bind( f, v, _1 );
}
template< typename R, typename V1, typename V2 >
boost::function< boost::function< R ( V1 ) > ( V2 ) > curry(
boost::function< R ( V1, V2 ) > f ) {
        return boost::bind( papply< R, V1, V2 >, f, _1 );
}

int add( int a, int b ) { return a + b; }

int main() {
        boost::function< int ( int, int ) > fadd( add );
        boost::function< boost::function< int ( int ) > ( boost::function< int
( int, int ) >, int ) > fpapply( papply< int, int, int > );

        boost::function< int ( int ) > inc1( papply( fadd, 1 ) );
        std::cout << inc1( 6 ) << " " << inc1( 10 ) << std::endl;

        boost::function< int ( int ) > inc2( fpapply( fadd, 1 ) );
        std::cout << inc2( 6 ) << " " << inc2( 10 ) << std::endl;

        boost::function< boost::function< int ( int ) > ( int ) > curried_add(
curry( fadd ) );
        boost::function< int ( int ) > inc3( curried_add( 1 ) );
        std::cout << inc3( 6 ) << " " << inc3( 10 ) << std::endl;

        //curry( fpapply );
}

Compiling this on MSVC 8.1 SP1 from Boost 1.34.0 RC. I get a nearly
130-line error message, the last part of which is this:

1>e:\dev\boost\install\basic\include\boost-1_34\boost\bind.hpp(278) :
error C2664: 'boost::function<Signature>
(boost::function<boost::function<int (int)> (boost::function<int
(int,int)>,int)>,V1)' : cannot convert parameter 1 from 'int' to
'boost::function<Signature>'
1> with
1> [
1> Signature=boost::function<int (int)> (int),
1> V1=boost::function<int (int,int)>
1> ]
1> and
1> [
1> Signature=boost::function<int (int)> (boost::function<int
(int,int)>,int)
1> ]
1> No constructor could take the source type, or constructor
overload resolution was ambiguous

On my web site is the same program (at least the same papply and curry
functions), but using Boost.Lambda (there's also a bit of an explanation
about what they do).

http://www.kirit.com/Blog:/2007-09-10/What%20do%20you%20get%20when%20you%20curry%20partial%20application%3F

The Boost.Lambda error is even longer at nearly 200 lines. As both
libraries give errors I'm inclined to think that I've done something
stupid, but both papply() and curry() appear to work.

Have I hit a limit on what these two libraries can do, or was there a
fix for this between the 1.34.0 RC and 1.34.1 versions?

K


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net