Static functions from boost.lambda or boost.phoenix
Asked Answered
B

1

8

I regularly use boost.lambda (and phoenix) to define lambda functions in C++. I really like their polymorphic property, the simplicity of their representation and the way they make functional programming in C++ so much easier. In some cases, it's even cleaner and more readable (if you're used to reading them) to use them for defining small functions and naming them in the static scope.

The way to store these functionals that resembles conventional functions the most is to capture them in a boost::function

const boost::function<double(double,double)> add = _1+_2;

But the problem is the runtime inefficiency of doing so. Even though the add function here is stateless, the returned lambda type is not empty and its sizeof is greater than 1 (so boost::function default ctor and copy ctor will involve new). I really doubt that there's a mechanism from the compiler's or the boost's side to detect this statelessness and generate code which is equivalent to using:

double (* const add)(double,double) = _1+_2; //not valid right now

One could of course use the c++11 auto, but then the variable can't be passed around non-templated contexts. I've finally managed to do almost what I want, using the following approach:

#include <boost/lambda/lambda.hpp>
using namespace boost::lambda;

#include <boost/type_traits.hpp>
#include <boost/utility/result_of.hpp>
using namespace boost;


template <class T>
struct static_lambda {

    static const T* const t;

    // Define a static function that calls the functional t
    template <class arg1type, class arg2type>
    static typename result_of<T(arg1type,arg2type)>::type 
        apply(arg1type arg1,arg2type arg2){
        return (*t)(arg1,arg2); 
    }

    // The conversion operator
    template<class func_type>
    operator func_type*() {
       typedef typename function_traits<func_type>::arg1_type arg1type;
       typedef typename function_traits<func_type>::arg2_type arg2type;
       return &static_lambda<T>::apply<arg1type,arg2type>;
    }
};

template <class T>
const T* const static_lambda<T>::t = 0;

template <class T>
static_lambda<T> make_static(T t) {return static_lambda<T>();}

#include <iostream>
#include <cstdio>


int main() {
    int c=5;
    int (*add) (int,int) = make_static(_1+_2);
    // We can even define arrays with the following syntax
    double (*const func_array[])(double,double) = {make_static(_1+_2),make_static(_1*_2*ref(c))};
    std::cout<<func_array[0](10,15)<<"\n";
    std::fflush(stdout);
    std::cout<<func_array[1](10,15); // should cause segmentation fault since func_array[1] has state
}

Compiled with gcc 4.6.1 The output from this program is (regardless of the optimization level):

25
Segmentation fault

as expected. Here, I'm keeping a static pointer to the lambda expression type (as const as possible for optimization purposes) and initializing it to NULL. This way, if you try to "staticify" a lambda expression with state, you're sure to get a runtime error. And if you staticify a genuinely stateless lambda expression, everything works out.

On to the question(s):

  1. The method seems a bit dirty, can you think of any circumstance, or compiler assumption that will make this misbehave (expected behavior: work fine if lambda is stateless, segfault otherwise).

  2. Can you think of any way that attempting this will cause a compiler error instead of a segfault when the lambda expression has state?

EDIT after Eric Niebler's answer:

#include <boost/phoenix.hpp>
using namespace boost::phoenix;
using namespace boost::phoenix::arg_names;

#include <boost/type_traits.hpp>
#include <boost/utility/result_of.hpp>
using boost::function_traits;

template <class T>
struct static_lambda {
    static const T t;

    // A static function that simply applies t
    template <class arg1type, class arg2type>
    static typename boost::result_of<T(arg1type,arg2type)>::type 
    apply(arg1type arg1,arg2type arg2){
    return t(arg1,arg2); 
    }

    // Conversion to a function pointer
    template<class func_type>
    operator func_type*() {
    typedef typename function_traits<func_type>::arg1_type arg1type;
        typedef typename function_traits<func_type>::arg2_type arg2type;
        return &static_lambda<T>::apply<arg1type,arg2type>;
    }
};

template <class T>
const T static_lambda<T>::t; // Default initialize the functional

template <class T>
static_lambda<T> make_static(T t) {return static_lambda<T>();}

#include <iostream>
#include <cstdio>


int main() {
    int (*add) (int,int) = make_static(_1+_2);

    std::cout<<add(10,15)<<"\n";

    int c=5;

    // int (*add_with_ref) (int,int) = make_static(_1+_2+ref(c)); causes compiler error as desired
}
Battle answered 13/4, 2012 at 16:46 Comment(2)
IIRC, if you're using Phoenix, you can store the result inside of a boost::phoenix::function rather than a boost::function and mitigate some efficiency loss (boost::phoenix::function are POD types and can be statically initialized at compile-time).Donell
@Donell Thanks for the heads up about boost::phoenix::function, that's bound to be useful in many cases. I'm still interested in getting native-equivalent (runtime-performance wise) lambda function capturing though. I'm not sure if it's at all possible to make this production quality, but I find the pursuit interesting.Battle
T
11
  1. There is no way to make this cleaner. You are calling a member function through a null pointer. This is all kinds of undefined behavior, but you know that already.
  2. You can't know if a Boost.Lambda function is stateless. It's a black box. Boost.Phoenix is a different story. It's built on Boost.Proto, a DSL toolkit, and Phoenix publishes its grammar and gives you hooks to introspect the lambda expressions it generates. You can quite easily write a Proto algorithm to look for stateful terminals and bomb out at compile time if it finds any. (But that doesn't change my answer to #1 above.)

You said you like the polymorphic nature of Boost's lambda functions, but you're not using that property in your code above. My suggestion: use C++11 lambdas. The stateless ones already have an implicit conversion to raw function pointers. It's just what you're looking for, IMO.

===UPDATE===

Although calling a member function through a null pointer is a terrible idea (don't do it, you'll go blind), you can default-construct a NEW lambda object of the same type of the original. If you combine that with my suggestion in #2 above, you can get what you're after. Here's the code:

#include <iostream>
#include <type_traits>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/and.hpp>
#include <boost/phoenix.hpp>

namespace detail
{
    using namespace boost::proto;
    namespace mpl = boost::mpl;

    struct is_stateless
      : or_<
            when<terminal<_>, std::is_empty<_value>()>,
            otherwise<
                fold<_, mpl::true_(), mpl::and_<_state, is_stateless>()>
            >
        >
    {};

    template<typename Lambda>
    struct static_lambda
    {
        template<typename Sig>
        struct impl;

        template<typename Ret, typename Arg0, typename Arg1>
        struct impl<Ret(Arg0, Arg1)>
        {
            static Ret apply(Arg0 arg0, Arg1 arg1)
            {
                return Lambda()(arg0, arg1);
            }
        };

        template<typename Fun>
        operator Fun*() const
        {
            return &impl<Fun>::apply;
        }
    };

    template<typename Lambda>
    inline static_lambda<Lambda> make_static(Lambda const &l)
    {
        static_assert(
            boost::result_of<is_stateless(Lambda)>::type::value,
            "Lambda is not stateless"
        );
        return static_lambda<Lambda>();
    }
}

using detail::make_static;

int main()
{
    using namespace boost::phoenix;
    using namespace placeholders;

    int c=5;
    int (*add)(int,int) = make_static(_1+_2);

    // We can even define arrays with the following syntax
    static double (*const func_array[])(double,double) = 
    {
        make_static(_1+_2),
        make_static(_1*_2)
    };
    std::cout << func_array[0](10,15) << "\n";
    std::cout << func_array[1](10,15);

    // If you try to create a stateless lambda from a lambda
    // with state, you trigger a static assertion:
    int (*oops)(int,int) = make_static(_1+_2+42); // ERROR, not stateless
}

Disclaimer: I am not the author of Phoenix. I don't know if default-constructability is guaranteed for all stateless lambdas.

Tested with MSVC-10.0.

Enjoy!

Turbulence answered 7/9, 2012 at 22:26 Comment(4)
I've just seen your update, I've also tried to use Phoenix lambdas after reading your original answer and noticed that they support default construction on stateless lambdas. I've also come up with a solution that uses that property (check my edit). Though I think your solution is better and more didactic :)Battle
Careful there. It's not only stateless Phoenix lambdas that are default-constructable; your solution won't catch lambdas that capture locals by value, so long as the types of those locals are themselves default-constructable. You really need to use the is_stateless Proto algorithm I wrote above.Turbulence
@EricNiebler I've just tested compiling int (*add) (int,int) = make_static(_1+_2+localVariable); and it doesn't compile (as desired). Are you sure about your last comment? Is it about the boost version (mine is 1.48)? Or are you talking about something else? BTW, I still think that your is_stateless metafunction is very useful.Battle
@EricNiebler About the polymorphic property: I know I'm not using that in the code above, but you can imagine a case where you need to pass a polymorphic lambda (as a visitor) to a templated function, and that function instantiates your lambda with different types and stores them as regular function pointers. You can't use c++11 lambdas in this extreme case :)Battle

© 2022 - 2024 — McMap. All rights reserved.