Static functions from boost.lambda or boost.phoenix

荒凉一梦 提交于 2019-12-19 16:47:32

问题


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
}

回答1:


  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!



来源:https://stackoverflow.com/questions/10144948/static-functions-from-boost-lambda-or-boost-phoenix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!