Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:40:11+00:00 2026-06-11T12:40:11+00:00

I regularly use boost.lambda (and phoenix) to define lambda functions in C++. I really

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:40:13+00:00Added an answer on June 11, 2026 at 12:40 pm
    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a shared template that i use regularly to show errors like :
I just realized all the data structures I regularly use are really old and
I have several functions that I wrote and I use regularly on my servers,
I have these variables: boost::regex re //regular expression to use std::string stringToChange //replace this
I use CSS transitions fairly regularly and for some reason I cannot get them
I have a WinForms utility that I use constantly, and enhance regularly. Roughly one
I want to use regular expression same way as string.Format. I will explain I
I'm trying to use regular expression right now and I'm really confuse. I want
I regularly use the jQuery jScrollPane plugin to add custom scroll bars to page
In the past I have created ItemTemplates for classes that I regularly use in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.