question about odd-looking template parameter

B

BekTek

when I worked with boost::function library.. I saw some odd parameter
passing..

look like this..
boost::function<int (int,int)> f;
.....

Is it valid statement in terms of c++ standard..?
What type is [ int (int,int) ] ?

Could someone tell me more detail?
 
D

Dietmar Kuehl

BekTek said:
boost::function<int (int,int)> f;

Is it valid statement in terms of c++ standard..?
What type is [ int (int,int) ] ?

This stumped me at first, too, especially in the forum used by the
'result_of' class template, where it was used for a functor type 'F'
like this: 'result_of<F(int, int)>'. Although this looks rather odd,
it is indeed legal C++: the template argument is actually a function
type. More precisely, 'int(int, int)' is the type of a function
taking two 'int's as argument and returning an 'int'. For the
'function' class template, this essentially gives the signature of
the functions being abstracted by the instantiation. In case of the
'result_of' class template, it is actually somewhat of an abuse:
since the return type is to be determined by 'result_of', the
"return type" of the template argument is actually used as the
functor type. This becomes specially funny if the functor type is
actually a function type.
 
B

BekTek

Dietmar Kuehl said:
BekTek said:
boost::function<int (int,int)> f;

Is it valid statement in terms of c++ standard..?
What type is [ int (int,int) ] ?

This stumped me at first, too, especially in the forum used by the
'result_of' class template, where it was used for a functor type 'F'
like this: 'result_of<F(int, int)>'. Although this looks rather odd,
it is indeed legal C++: the template argument is actually a function
type. More precisely, 'int(int, int)' is the type of a function
taking two 'int's as argument and returning an 'int'.

Alrighty, thanks.. I got it..
For the
'function' class template, this essentially gives the signature of
the functions being abstracted by the instantiation. In case of the
'result_of' class template, it is actually somewhat of an abuse:
since the return type is to be determined by 'result_of', the
"return type" of the template argument is actually used as the
functor type. This becomes specially funny if the functor type is
actually a function type.
--

Could tell me more about that in detail?
I'm still confused how boost::function works..
for example, in boost::function<int (char)> class it has typedef
function_return_type<R>::type internal_return_type;
But I think R is [int (char)], right?
How can [int (char)] be return type ? the return type should be [int].. I
suppose..
What treaks boost use in this case?
 
D

Dietmar Kuehl

BekTek said:
Could tell me more about that in detail?

Sure: I'm currently writing a book about the C++ library technical
report
(aka TR1) which includes 'std::tr1::function'. This is essentially just
'boost::function' but in another namespace :)
for example, in boost::function<int (char)> class it has typedef
function_return_type<R>::type internal_return_type;

First of all, 'internal_return_type' seems to be an implementation
detail
as far as I can tell, judging e.g. from the name of the type. It is, in
any case, not part of the TR1 specification. This has 'result_type' as
the only publically visible typedef.
But I think R is [int (char)], right?

I don't have the Boost implementation in front of me but I do have the
TR1
specification and this uses 'R' as the return type, e.g. in the form:

R(T1, T2, ..., TN)

Thus, I would guess that 'function_return_type<R>' somehow process the
function's return type to yield the actual return type (e.g. it might
add or remove a reference and/or const qualification). However, it is
rather unlikely that

function_return_type<R>::type

is identical to

R
How can [int (char)] be return type ?

In C++ it can't, at least it can't yet (maybe a future version of C++
support Lambda expression which would indeed yield function; currently
you can only create functor as is done by Boost's Lambda library). The
suitable return type is somehow deduced from 'R'. Assuming 'R' is
indeed
'int(char)', you can deduce the type from a class template like this:

template <typename T> struct function_return_type; // general version
template <typename R, T>
struct function_return_type<R(T)> { typedef R type; };
// more partial specializations, e.g. for function with more
arguments

If this is what 'function_return_type' does, it is much like TR1's
'result_of'.
the return type should be [int].. I
suppose..

.... and it probably is.
What treaks boost use in this case?

It probably uses partial specialization for 'function_return_type' to
yield the desired type (or some other type used internally by
'function_return_type').
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top