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; }
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; }
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.
From Barry's comment on sehe's answer:
#include "boost/lambda/lambda.hpp"
...
auto returnThree = boost::lambda::constant(3);