Passing lambda placeholders to functions
It seems very simple, but it's not compiling. int translate(const std::string& str) { return 0; } //all required using declarations are present but omitted for brevity // options_description options; variables_map values; int i; options.add_options() ("test", valuestd::string()->notifier(var(i) = translate(_1))); I get the following error: 'translate': cannot convert parameter 1 from 'boost::lambda::placeholder1_type' to 'std::string'. No user defined conversion operator available that can perform this conversion, or the operator cannot be called. Is this type of usage unsupported for some reason? If I change the variable declaration and the add_options() call to the following it works fine: std::string s options.add_options() ("test", valuestd::string()->notifier(var(s) = _1));
AMDG Zachary Turner wrote:
It seems very simple, but it's not compiling.
int translate(const std::string& str) { return 0; }
//all required using declarations are present but omitted for brevity // options_description options; variables_map values; int i;
options.add_options() ("test", valuestd::string()->notifier(var(i) = translate(_1)));
I get the following error:
'translate': cannot convert parameter 1 from 'boost::lambda::placeholder1_type' to 'std::string'. No user defined conversion operator available that can perform this conversion, or the operator cannot be called.
Is this type of usage unsupported for some reason?
It's unsupported because C++ doesn't allow Boost.Lambda to magically create overloads of every function that you might want to use with it. Not to mention that if this usage did work, then trying to pass a lambda functor to a function would be ambiguous with trying to compose a larger lambda expression. Use bind. http://tinyurl.com/nkhuez.
If I change the variable declaration and the add_options() call to the following it works fine:
std::string s
options.add_options() ("test", valuestd::string()->notifier(var(s) = _1));
In Christ, Steven Watanabe
On Mon, Jun 22, 2009 at 6:31 PM, Steven Watanabe
AMDG
Zachary Turner wrote:
It seems very simple, but it's not compiling.
int translate(const std::string& str) { return 0; }
//all required using declarations are present but omitted for brevity // options_description options; variables_map values; int i;
options.add_options() ("test", valuestd::string()->notifier(var(i) = translate(_1)));
I get the following error:
'translate': cannot convert parameter 1 from 'boost::lambda::placeholder1_type' to 'std::string'. No user defined conversion operator available that can perform this conversion, or the operator cannot be called.
Is this type of usage unsupported for some reason?
It's unsupported because C++ doesn't allow Boost.Lambda to magically create overloads of every function that you might want to use with it. Not to mention that if this usage did work, then trying to pass a lambda functor to a function would be ambiguous with trying to compose a larger lambda expression. Use bind. http://tinyurl.com/nkhuez.
Thanks. On a related note, if a lambda placeholder refers to a structure, is there any way to access its members short of using bind and making a fake function to perform the access? For example struct foo { int x; int y; }; struct bar { int x, int y; }; foo f; options.add_options() ("test", value<bar>()->notifier( var(f.x) = _1.x))); is what I want. I've tried a variety of different things, most promising seemed to be options.add_options() ("test", value<bar>()->notifier( var(f.x) = bind(&bar::x, _1))); but this doesn't work either. The only way I was able to get it to work was to make a function void copy_value(foo& dest, const bar& src) { dest.x = src.x; } and then doing options.add_options() ("test", value<bar>()->notifier( bind(copy_value, f, _1)); Is this my only hope?
AMDG Zachary Turner wrote:
Thanks. On a related note, if a lambda placeholder refers to a structure, is there any way to access its members short of using bind and making a fake function to perform the access?
For example
struct foo { int x; int y; }; struct bar { int x, int y; };
foo f;
options.add_options() ("test", value<bar>()->notifier( var(f.x) = _1.x)));
is what I want. I've tried a variety of different things, most promising seemed to be
options.add_options() ("test", value<bar>()->notifier( var(f.x) = bind(&bar::x, _1)));
but this doesn't work either.
It ought to work. What's the error?
The following works for me:
#include
On Tue, Jun 23, 2009 at 2:52 PM, Steven Watanabe
AMDG
Zachary Turner wrote:
Thanks. On a related note, if a lambda placeholder refers to a structure, is there any way to access its members short of using bind and making a fake function to perform the access?
For example
struct foo { int x; int y; }; struct bar { int x, int y; };
foo f;
options.add_options() ("test", value<bar>()->notifier( var(f.x) = _1.x)));
is what I want. I've tried a variety of different things, most promising seemed to be
options.add_options() ("test", value<bar>()->notifier( var(f.x) = bind(&bar::x, _1)));
but this doesn't work either.
It ought to work. What's the error?
The following works for me:
#include
#include #include <iostream> struct foo { int x; int y; }; struct bar { int x; int y; };
int main() { foo f = { 1, 2 }; const bar b = { 3, 4 }; (boost::lambda::var(f.x) = boost::lambda::bind(&bar::x, boost::lambda::_1))(b); std::cout << f.x << std::endl; }
Ok so the problem is that I was using boost::bind instead of boost::lambda::bind. Should only boost::lambda::bind be used inside lambda expressions? And why isn't there just a single bind that works in all cases?
Zachary Turner wrote:
Ok so the problem is that I was using boost::bind instead of boost::lambda::bind. Should only boost::lambda::bind be used inside lambda expressions?
Yep.
And why isn't there just a single bind that works in all cases?
Good question. We're working on it. Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
participants (3)
-
Joel de Guzman
-
Steven Watanabe
-
Zachary Turner