 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
E Gladyshev Guest
|
Posted: Thu Jul 08, 2004 11:52 am Post subject: named parameters |
|
|
Does anybody know about boost::function<> kind of
type that supports named parameters?
AFAIK typically named parameters in C++ are simulated
with methods. For example:
struct window
{
something& title( const string& t );
something& style( int s );
};
What I am looking for is something that
could be used to convert any function
(including standalone) into a type with named
parameters w/o writing any methods or functions.
(type declarations are fine).
What I am proposing is this.
// there is a C function that is implemented somewhere.
//
int CreateWidget( const char* title, int style );
// define argument names
//
struct title {};
struct style {};
// declare a function that returns 'int' and accepts
// named paramters 'title' and 'style'
typedef function_proxy<
int //the function returns 'int'
(
//'argument name', 'argument type'
title, const char*
,style, numeric_argument
)
int main()
{
create_widget cw(CreateWidget);
//call with 'style' 3 and title 'nill'
cw( create_widget::arg<style>(3) );
//call with 'title' "hi" and 'style' 45
cw( create_widget::arg<title>("hi") );
//call with 'title' "hi" and 'style' 5
//note the order of parameters doesn't matter only names
cw(
,create_widget::arg<style>(5)
create_widget::arg<title>("hi") );
return 0;
};
create_widget<> can be used with any function or
method that have the specified signature.
I already have a working prototype of the function_proxy<>
template but anybody has a better solution?
Eugene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Fri Jul 09, 2004 1:19 pm Post subject: Re: named parameters |
|
|
E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
| Quote: | Does anybody know about boost::function<> kind of
type that supports named parameters?
AFAIK typically named parameters in C++ are simulated
with methods. For example:
struct window
{
something& title( const string& t );
something& style( int s );
};
What I am looking for is something that
could be used to convert any function
(including standalone) into a type with named
parameters w/o writing any methods or functions.
(type declarations are fine).
|
You might look at: http://tinyurl.com/2qtkk Which will be reviewed in
not too long. It's not quite what you're asking for, but provides a
much nicer interface for the named parameters part. Maybe you could
combine it with boost::function to provide a nice front-end.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
E Gladyshev Guest
|
Posted: Sat Jul 10, 2004 10:54 am Post subject: Re: named parameters |
|
|
David Abrahams wrote:
| Quote: | E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
What I am looking for is something that
could be used to convert any function
(including standalone) into a type with named
parameters w/o writing any methods or functions.
(type declarations are fine).
You might look at: http://tinyurl.com/2qtkk Which will be reviewed in
not too long. It's not quite what you're asking for,
|
No, not quite!
| Quote: | but provides a
much nicer interface for the named parameters part.
|
I am not sure about that.
Basically the only difference is in how you define
parameters and pass them around.
* My solution.
You define generic names that are associated with
the semantic not a concrete type.
struct title {};
struct style {};
and pass them as
arg<title>("hi")
arg<style>(5)
Which is not as nice as title="hi".
* Your solution
You associate parameters with the types, not semantic.
struct title_t;
struct style_t;
typedef keyword<title_t> title;
typedef keyword<style_t> style;
I don't consider this part as pretty at all.
For instance if many of yout functions have
"style" parameters of different types
you would have to do something like
keyword<int> style_int;
keyword<char*> style_char;
| Quote: | Maybe you could
combine it with boost::function to provide a nice front-end.
|
This is basically what I have already done.
As you might have already guessed the internal
portion of my class is implemented similarly
to the NamedParams lib
(I just didn't know about its existence).
I'll probably post my implementation shortly
as part of my experimental library, TTL at
http://sourceforge.net/projects/tinytl/
Eugene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Sun Jul 11, 2004 10:11 am Post subject: Re: named parameters |
|
|
E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
| Quote: | David Abrahams wrote:
E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
What I am looking for is something that
could be used to convert any function
(including standalone) into a type with named
parameters w/o writing any methods or functions.
(type declarations are fine).
You might look at: http://tinyurl.com/2qtkk Which will be reviewed in
not too long. It's not quite what you're asking for,
No, not quite!
but provides a
much nicer interface for the named parameters part.
I am not sure about that.
Basically the only difference is in how you define
parameters and pass them around.
* My solution.
You define generic names that are associated with
the semantic not a concrete type.
struct title {};
struct style {};
|
As in mine.
| Quote: | and pass them as
arg<title>("hi")
arg<style>(5)
Which is not as nice as title="hi".
* Your solution
You associate parameters with the types, not semantic.
|
Incorrect. I can't imagine what would make you say that. My
keywords are as associated with semantics as yours are.
| Quote: | struct title_t;
struct style_t;
typedef keyword<title_t> title;
typedef keyword<style_t> style;
I don't consider this part as pretty at all.
|
Correct; it takes a little bit more work on the part of the person
providing the named parameter interface, in order to make the syntax
for those using the interface much nicer.
| Quote: | For instance if many of yout functions have
"style" parameters of different types
you would have to do something like
keyword<int> style_int;
keyword<char*> style_char;
|
Incorrect. I think I see your misconception. You think the user will
be passing a title_t to the function above. She won't; it's just a
tag type, as in your interface.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
E Gladyshev Guest
|
Posted: Sat Jul 17, 2004 10:18 am Post subject: Re: named parameters |
|
|
David Abrahams wrote:
| Quote: | E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
David Abrahams wrote:
[...]
Incorrect. I think I see your misconception. You think the user will
be passing a title_t to the function above. She won't; it's just a
tag type, as in your interface.
|
I was out of town. Sorry for the late reply.
You are right! I spoke too soon.
I thought that title_t is an "actual" type.
Are there any plans to marry the boost::function
and Named Parameters semantics, something similar
to my suggestion?
Eugene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Sun Jul 18, 2004 10:49 am Post subject: Re: named parameters |
|
|
E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
| Quote: | David Abrahams wrote:
E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
David Abrahams wrote:
[...]
Incorrect. I think I see your misconception. You think the user will
be passing a title_t to the function above. She won't; it's just a
tag type, as in your interface.
I was out of town. Sorry for the late reply.
You are right! I spoke too soon.
I thought that title_t is an "actual" type.
Are there any plans to marry the boost::function
and Named Parameters semantics, something similar
to my suggestion?
|
There are no such plans, but frankly I'm not sure what you're after.
Maybe you just want a named-parameter font-end to a boost::function?
If so that would probably be easy to achieve by deriving a new
function object from a boost::function and giving it a
named-parameter interface. I don't think there's much you could do
with a library to make it less work for the designer of such a
function object *other* than by doing it with macros, though I could
be wrong.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
E Gladyshev Guest
|
Posted: Mon Jul 19, 2004 10:24 am Post subject: Re: named parameters |
|
|
David Abrahams wrote:
| Quote: | E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
David Abrahams wrote:
E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
David Abrahams wrote:
[...]
Incorrect. I think I see your misconception. You think the user will
be passing a title_t to the function above. She won't; it's just a
tag type, as in your interface.
I was out of town. Sorry for the late reply.
You are right! I spoke too soon.
I thought that title_t is an "actual" type.
Are there any plans to marry the boost::function
and Named Parameters semantics, something similar
to my suggestion?
There are no such plans, but frankly I'm not sure what you're after.
Maybe you just want a named-parameter font-end to a boost::function?
If so that would probably be easy to achieve by deriving a new
function object from a boost::function and giving it a
named-parameter interface.
|
In my experiment, I actually encapsulated
boost::function (or ttl::function) but
you could as well derive from it.
| Quote: | I don't think there's much you could do
with a library to make it less work for the designer of such a
function object *other* than by doing it with macros, though I could
be wrong.
|
The idea is to provide a simple way for creating
a named parameter functor from any *existing* function.
Let's consider the Named Params library.
Say, we have a third party library that has
void foo3rdparty1( const char* name, int value );
(foo3rdparty1() source isn't available).
AFAIU your library, you would have to create a wrapper.
BOOST_NAMED_PARAMS_FUN(void, foo1, 0, 2, foo_keywords)
{
foo3rdparty1( p[name], p[value] );
}
int main()
{
foo1( value = 3 );
return 0;
};
You would have to write a separate wrapper for any 3rd party
function with the same signature and semantic.
BOOST_NAMED_PARAMS_FUN(void, foo2, 0, 2, foo_keywords)
{
foo3rdparty2( p[name], p[value] );
}
int main()
{
foo1( value = 3 );
foo2( value = 3 );
return 0;
};
You could probably declare a functor
typedef function< const char*, int > foo_functor;
and the named params wrapper accept the functor parameter.
I think that a better way is to implement a generic
functor class with a native support of named parameters.
For example if we use the syntax from my library,
you declare a generic functor just once and the
parameter dispatching is handled automatically
and no macros is necessary.
struct name;
struct value;
typedef function_proxy
<
name, const char*
, value, int
int main()
{
foo f1 = foo3rdparty1;
foo f2 = foo3rdparty2;
f1( foo::arg
f2( foo::arg<value>(3) );
return 0;
};
I would definetely like f1( value = 3 ) instead of
f1( foo::arg<value>(3) );
Basically it is a tradeoff.
If you want the value=3 syntax,
the user has to write a not very pretty
code while declaring his named parameters.
struct value_t;
typedef keyword<value_t> value;
If you go with something like foo::arg<value>(3), then
struct value;
is enough to name the parameter.
I personally prefer value=3. In this case,
the user of the named params functors doesn't have
to write the arg<...> stuff.
I think that it is a natural step to extend
the Named Params library to support generic
named params functors.
Eugene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
E Gladyshev Guest
|
Posted: Thu Aug 05, 2004 10:47 am Post subject: Re: named parameters |
|
|
David Abrahams wrote:
| Quote: | E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
David Abrahams wrote:
E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
David Abrahams wrote:
[...]
Incorrect. I think I see your misconception. You think the user will
be passing a title_t to the function above. She won't; it's just a
tag type, as in your interface.
I was out of town. Sorry for the late reply.
You are right! I spoke too soon.
I thought that title_t is an "actual" type.
Are there any plans to marry the boost::function
and Named Parameters semantics, something similar
to my suggestion?
There are no such plans, but frankly I'm not sure what you're after.
Maybe you just want a named-parameter font-end to a boost::function?
If so that would probably be easy to achieve by deriving a new
function object from a boost::function and giving it a
named-parameter interface. I don't think there's much you could do
with a library to make it less work for the designer of such a
function object *other* than by doing it with macros, though I could
be wrong.
|
I have submitted by code to TTL ([url]http://tinytl.sourceforge.net/)[/url].
See the named_params_function section at
http://tinytl.sourceforge.net/#func_named_params_function
A class member sample is in
samples/test/names_params_test.cpp
Eugene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Sun Aug 08, 2004 10:05 am Post subject: Re: named parameters |
|
|
E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
That's a really lovely interface. Good job!
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Sun Aug 08, 2004 10:02 pm Post subject: Re: named parameters |
|
|
David Abrahams <dave (AT) boost-consulting (DOT) com> writes:
| Quote: | E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
I have submitted by code to TTL ([url]http://tinytl.sourceforge.net/)[/url].
See the named_params_function section at
http://tinytl.sourceforge.net/#func_named_params_function
A class member sample is in
samples/test/names_params_test.cpp
That's a really lovely interface. Good job!
|
I mean, of course, the declaration interface:
typedef ttl::func::named_params_function<
int //the function returns 'int'
(
//'argument name', 'argument type'
title, const char*
,style, numeric_argument
)
The invocation interface is still pretty distasteful:
cw( cw.arg<title>("hi") );
^^^^^^^^^^^^^^ ^
I think it would be fairly easy to build your function object
declaration interface on top of our named params library.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
E Gladyshev Guest
|
Posted: Mon Aug 09, 2004 9:11 pm Post subject: Re: named parameters |
|
|
David Abrahams wrote:
| Quote: | David Abrahams <dave (AT) boost-consulting (DOT) com> writes:
E Gladyshev <egladyshev (AT) comcast-nosspam (DOT) net> writes:
I have submitted by code to TTL ([url]http://tinytl.sourceforge.net/)[/url].
See the named_params_function section at
http://tinytl.sourceforge.net/#func_named_params_function
A class member sample is in
samples/test/names_params_test.cpp
That's a really lovely interface. Good job!
I mean, of course, the declaration interface:
typedef ttl::func::named_params_function
int //the function returns 'int'
(
//'argument name', 'argument type'
title, const char*
,style, numeric_argument<int, 45> //the default is 45
)
create_widget;
The invocation interface is still pretty distasteful:
cw( cw.arg<title>("hi") );
^^^^^^^^^^^^^^ ^
|
Thanks for taking a look at it.
I totally agree, the invocation interface is pretty ugly.
| Quote: |
I think it would be fairly easy to build your function object
declaration interface on top of our named params library.
|
I think so. I'll give it a try.
Basically the arg<...> function creates something
similar to your keyword<>.
What's the Named Params library status?
Eugene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|