C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Templates: preferred vs portable syntax...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
PeterB
Guest





PostPosted: Mon Jan 24, 2005 11:22 am    Post subject: Templates: preferred vs portable syntax... Reply with quote



Hi,
I have a question regarding what is often referred to as the "preferred
syntax":
Tmpl<int (int, int)>
as in contrast to the "portable syntax":
Tmpl<int,int,int>

The portable syntax is easy for me to grok but how does the preferred syntax
work? How is this allowed? It is not a pointer to a function(?), so what is
the type of that expression? (I know that it means to denote a function
returning an int and taking two ints as arguments. But I do not understand
how it really works.) I checked Josuttis&Vandevoorde C++ Templates but was
not able to find an answer...

To being able to use the preferred syntax it is required(?) that the
compiler supports partial template specialization. Is that the only
requirement of the compiler?

Thanks in advance!
/PeterB



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
doug.gregor@gmail.com
Guest





PostPosted: Mon Jan 24, 2005 8:31 pm    Post subject: Re: Templates: preferred vs portable syntax... Reply with quote



The terms "preferred syntax" and "portable syntax" are not general C++
terms; they refer to the two different syntax possibilities for the
Boost Function and Signals libraries.

When you write Tmpl<int(int, int)>, you are passing a function type to
"Tmpl". It's not a function pointer type, but the type of thing that a
function pointer points to. Function types are strange, because you
never really see them in normal usage, although you can write them as
above or typedef them like this:

typedef int F(int, int);

When we use the preferred syntax of Function/Signals, we're actually
forming a type like "F" above (but without a name).

As for compiler support... the lack of class template partial
specialization is one barrier to supporting the preferred syntax in
Function/Signals. Also, not all compilers know how to parse a type like
"int(int, int)".

Doug Gregor


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
PeterB
Guest





PostPosted: Tue Jan 25, 2005 10:43 am    Post subject: Re: Templates: preferred vs portable syntax... Reply with quote



<doug.gregor (AT) gmail (DOT) com> wrote:
Quote:

When you write Tmpl<int(int, int)>, you are passing a
function type to "Tmpl".

Aha, so it is a function type! Of course...

Quote:
It's not a function pointer type, but the type of thing
that a function pointer points to. Function types are
strange, because you never really see them in normal usage,

Indeed. I have read quite a lot of the available C++ literature
but have never seen them before reading the Boost docs.
I've checked Stroustrup, Sutter, Meyer, Alexandrescu,
Lippman, Koenig, Dewhurst, Vandervoorde&Josuttis,...
but found nothing on function types in that form.

Is that form of function types described somewhere? (Other
than the spec, in which they must be described. At least
implicitly...) If not, maybe boost function/signal could
explain them a bit?

Quote:
As for compiler support... the lack of class template partial
specialization is one barrier to supporting the preferred syntax
in Function/Signals. Also, not all compilers know how to parse a
type like "int(int, int)".

It is probably trivial, but I do not see in what way class
template partial specialization is involved with the case
of the preferred syntax. Could you please elaborate?

Thanks!
/Peter



[ 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





PostPosted: Tue Jan 25, 2005 8:00 pm    Post subject: Re: Templates: preferred vs portable syntax... Reply with quote

"PeterB" <peter (AT) nospammm (DOT) best.com> writes:

Quote:
doug.gregor (AT) gmail (DOT) com> wrote:

When you write Tmpl<int(int, int)>, you are passing a
function type to "Tmpl".

Aha, so it is a function type! Of course...

It's not a function pointer type, but the type of thing
that a function pointer points to. Function types are
strange, because you never really see them in normal usage,

Indeed. I have read quite a lot of the available C++ literature
but have never seen them before reading the Boost docs.
I've checked Stroustrup, Sutter, Meyer, Alexandrescu,
Lippman, Koenig, Dewhurst, Vandervoorde&Josuttis,...
but found nothing on function types in that form.

Is that form of function types described somewhere? (Other
than the spec, in which they must be described. At least
implicitly...) If not, maybe boost function/signal could
explain them a bit?

They're discussed in chapter 2 of
http://www.boost-consulting.com/metaprogramming-book.html

Quote:
As for compiler support... the lack of class template partial
specialization is one barrier to supporting the preferred syntax
in Function/Signals. Also, not all compilers know how to parse a
type like "int(int, int)".

It is probably trivial, but I do not see in what way class
template partial specialization is involved with the case
of the preferred syntax. Could you please elaborate?

Basically, boost::function<some_function_type> needs to separate all the
types involved in some_function_type from one another, so that it can
use them in the same way function3 does, for example. In other words,
within the body of function3:

template <class Return, class Param1, class Param2, class Param3>
struct function3
{
...
};

the identity of Return, Param1, Param2, and Param3 are all known. But
when you write:

function<int(char,long,int*)>

the function template only sees a single function type:
"int(char,long,int*)". The only way to get the types int, char, long,
int* at compile-time is to use partial specialization to do "pattern
matching" that extracts those types:

template <class F>
struct function
// { // If uncommented, this would be a primary template
// // definition and F would be seen as one monolithic
// } // function type here.
;


// Partial Specialization
template <class Return, class Param1, class Param2, class Param3>
struct function<Return(Param1, Param2, Param3)>
{
// types are known here.
};

It's a basic property of compound types that if you want to
"decompose" them, you need to do this sort of thing with partial
specialization. That's why, for example, the following boost type
traits can't work (without user assistance) on compilers that don't
support partial specialization:

remove_pointer [T* -> T]
remove_reference [T& -> T]
remove_const [T const -> T]
remove_volatile [T volatile -> T]
remove_bounds [T[N] -> T]

On those compilers, there are all sorts of nasty tricks you can use to
sort-of work around the lack of partial specialization, but this type
decomposition limitation just can't be overcome.

HTH,
--
Dave Abrahams
Boost Consulting
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
Hyman Rosen
Guest





PostPosted: Tue Jan 25, 2005 8:16 pm    Post subject: Re: Templates: preferred vs portable syntax... Reply with quote

PeterB wrote:
Quote:
Is that form of function types described somewhere?

Function types are the same in C++ as in C, and they
have existed in this form just about forever. They're
simple enough to understand. Take any ordinary function
declaration and remove all the identifiers. Voila.

The reason people like them for generic programming is
that they are something which can be passed as a single
argument to a template and represent a list of types and
can be written simply.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
PeterB
Guest





PostPosted: Wed Jan 26, 2005 6:43 pm    Post subject: Re: Templates: preferred vs portable syntax... Reply with quote

"David Abrahams" <dave (AT) boost-consulting (DOT) com> wrote:
Quote:

They're discussed in chapter 2 of
http://www.boost-consulting.com/metaprogramming-book.html

I've got to check that out!

Quote:
Basically, boost::function<some_function_type> needs to
[snipped]


Very nice and lucid explanation David (as usual) - thanks a lot!

/Peter





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.