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):
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).
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
}
boost::phoenix::function
rather than aboost::function
and mitigate some efficiency loss (boost::phoenix::function
are POD types and can be statically initialized at compile-time). – Donellboost::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