What means: template <class F, class Tuple> struct result<F(Tuple)>{}; ?

Discussion in 'C++' started by Stuart, Mar 6, 2013.

  1. Stuart

    Stuart Guest

    Hello newsgroup,

    I've stumbled over this posting from
    comp.lang.c++.moderated. The code that gets me confused is:


    template <class Signature>
    struct result;

    template <class F, class Tuple>
    struct result<F(Tuple)>
    : fusion::result_of::at_c<N,Tuple>
    {};

    I don't know what kind of thing F(Tuple) in the partial (?)
    specialization of result is. Is this a function type? I have never seen
    this before, even though I've had my share of Vandevoorde and Josuttis's
    Guide to C++ Templates.

    Thanks in advance,
    Stuart
     
    Stuart, Mar 6, 2013
    #1
    1. Advertising

  2. On 2013-03-06 14:47, Juha Nieminen wrote:
    >
    > I might remember completely wrong, but IIRC "type(type)" is a weird
    > alternative for declaring a function pointer type (which would usually
    > be written as "type(*)(type)". I'm not exactly sure why the (*) part
    > can be left out.)


    That is true only in declarations of function parameters, where just as
    "array of T" is adjusted to "pointer to T", "function returning T" is
    adjusted to "pointer to function returning T".

    In other cases, they are different:

    typedef int T(); T e; // e is a function
    typedef int (*T)(); T e; // e is an object (a pointer to function)

    --
    Seungbeom Kim
     
    Seungbeom Kim, Mar 8, 2013
    #2
    1. Advertising

  3. Stuart

    Stefan Ram Guest

    Re: What means: template <class F, class Tuple> struct result<F(Tuple)> {}; ?

    Juha Nieminen <> writes:
    >Given that functions are not first-class citizens in C/C++, I really
    >don't understand what "e is a function" could even mean (other than it
    >meaning "e is a pointer to a function".)


    You understand »e is a pointer to a function«. Then, by renaming,
    you understand »f is a pointer to a function«, Now, let's call the
    pointee »e«. Then, e is a function.

    #include <iostream>
    #include <ostream>

    int main()
    { typedef int T(); T e;
    ::std::cout << sizeof( e )<< '\n'; }

    main.cpp [Error] ISO C++ forbids applying 'sizeof' to an expression of function type [-fpermissive]
     
    Stefan Ram, Mar 10, 2013
    #3
  4. Stuart

    Stefan Ram Guest

    Re: What means: template <class F, class Tuple> struct result<F(Tuple)> {}; ?

    Juha Nieminen <> writes:
    >don't understand what "e is a function" could even mean


    ISO/IEC 14882 says under the heading

    »4.3 Function-to-pointer conversion [conv.func]«

    »An lvalue of function type T can be converted to a
    prvalue of type "pointer to T."«

    The correspondence between the heading and the following
    sentence suggest that a »function« (in the context of
    run-time values) is »an (l)value of function type«.
     
    Stefan Ram, Mar 10, 2013
    #4
  5. Stuart

    Stefan Ram Guest

    Re: What means: template <class F, class Tuple> struct result<F(Tuple)> {}; ?

    -berlin.de (Stefan Ram) writes:
    >typedef int T();


    modernized:

    using T = int();
     
    Stefan Ram, Mar 10, 2013
    #5
  6. Re: What means: template <class F, class Tuple> structresult<F(Tuple)> {}; ?

    On Sat, 09 Mar 2013 23:53:59 +0000, Juha Nieminen wrote:

    > Seungbeom Kim <> wrote:
    >> typedef int T(); T e; // e is a function
    >> typedef int (*T)(); T e; // e is an object (a pointer to
    >> function)

    >
    > Given that functions are not first-class citizens in C/C++, I really
    > don't understand what "e is a function" could even mean (other than it
    > meaning "e is a pointer to a function".)


    Given
    typedef int func();
    then
    func foo;
    is an odd but perfectly legal way of declaring a function called foo that
    takes no arguments and returns an int.

    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Mar 10, 2013
    #6
  7. On 2013-03-10 04:21, Bart van Ingen Schenau wrote:
    > On Sat, 09 Mar 2013 23:53:59 +0000, Juha Nieminen wrote:
    >
    >> Seungbeom Kim <> wrote:
    >>> typedef int T(); T e; // e is a function
    >>> typedef int (*T)(); T e; // e is an object (a pointer to
    >>> function)

    >>
    >> Given that functions are not first-class citizens in C/C++, I really
    >> don't understand what "e is a function" could even mean (other than it
    >> meaning "e is a pointer to a function".)

    >
    > Given
    > typedef int func();
    > then
    > func foo;
    > is an odd but perfectly legal way of declaring a function called foo that
    > takes no arguments and returns an int.


    That's exactly what I meant.

    typedef int func();
    func foo; // declare first

    // use foo()

    int foo() { ... } // define later

    --
    Seungbeom Kim
     
    Seungbeom Kim, Mar 11, 2013
    #7
    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. Bernd Fuhrmann
    Replies:
    0
    Views:
    446
    Bernd Fuhrmann
    Feb 12, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,405
    Chris Fogelklou
    Apr 20, 2004
  3. michael
    Replies:
    4
    Views:
    494
    Jonathan Burd
    Jan 29, 2005
  4. A L
    Replies:
    1
    Views:
    516
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  5. Michael Tan
    Replies:
    32
    Views:
    1,022
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page