question about odd-looking template parameter

Discussion in 'C++' started by BekTek, Nov 24, 2004.

  1. BekTek

    BekTek Guest

    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?
    BekTek, Nov 24, 2004
    #1
    1. Advertising

  2. BekTek wrote:
    > 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.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
    Dietmar Kuehl, Nov 24, 2004
    #2
    1. Advertising

  3. BekTek

    BekTek Guest

    "Dietmar Kuehl" <> wrote in message
    news:...
    > BekTek wrote:
    > > 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?

    > <mailto:> <http://www.dietmar-kuehl.de/>
    > <http://www.contendix.com> - Software Development & Consulting
    >
    BekTek, Nov 24, 2004
    #3
  4. BekTek wrote:
    > > 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?


    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').
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
    Dietmar Kuehl, Nov 24, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    447
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    516
    tom_usenet
    Jul 24, 2003
  3. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,049
    Richard Heathfield
    Feb 18, 2007
  4. Stuart Redmann
    Replies:
    5
    Views:
    461
    Stuart Redmann
    Dec 14, 2007
  5. Isti
    Replies:
    2
    Views:
    686
Loading...

Share This Page