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
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With