boost bind or lambda functor that returns a constant

327 views Asked by At

Can I use boost::bind or the boost lambda library to create a functor that ignores its arguments and always returns a constant?

e.g. a functor with equivalent behaviour to:

int returnThree( SomeType arg ) { return 3; }
2

There are 2 answers

0
SimonD On BEST ANSWER

From Barry's comment on sehe's answer:

#include "boost/lambda/lambda.hpp"

...

auto returnThree = boost::lambda::constant(3);
1
sehe On

Sure, use

boost::phoenix::val(3);

See it Live On Coliru

#include <boost/phoenix.hpp>

namespace p = boost::phoenix;
using namespace p::arg_names;

int main()
{
    auto p = p::val(42);
    return p() + p(/*ignored:*/77);
}

Which returns 84 as the exitcode.