Would Anyone Use a _FuncRetType Operator?

Discussion in 'C Programming' started by Shao Miller, Feb 21, 2012.

  1. Shao Miller

    Shao Miller Guest

    Would anyone use a '_FuncRetType' operator, described briefly, below?

    Where the 'type-name' syntax would be extended to include:

    type-name:
    ...
    _FuncRetType ( type-name )
    _FuncRetType identifier

    Where 'identifier' would have to have been declared as a function type.

    Where the '_FuncRetType' operator would yield a 'type-name' that is the
    same type as the return type of either:
    - A function whose type is the 'type-name' operand
    - The function designated by the 'identifier' operand

    Then you might do:

    /* Like 'int x;' */
    _FuncRetType (int(void)) x;

    or:

    int foo(void);
    /* Like 'int x;' */
    _FuncRetType foo x;

    or:

    typedef int f_int_no_args(void);
    /* Like 'int x;' */
    _FuncRetType(f_int_no_args) x;

    or:

    /* Third-party-provided version1.h */
    /* Might be present as 'version.h' */
    int my_func(void);

    /* Third-party-provided version2.h */
    /* Might be present as 'version.h' */
    long long int my_func(void);

    /* user.c */
    #include "version.h"

    typedef _FuncRetType my_func rt_my_func;
    /* Like 'int x;' */
    rt_my_func x;

    x = my_func();
    Shao Miller, Feb 21, 2012
    #1
    1. Advertising

  2. Shao Miller <> writes:
    > Would anyone use a '_FuncRetType' operator, described briefly, below?
    >
    > Where the 'type-name' syntax would be extended to include:
    >
    > type-name:
    > ...
    > _FuncRetType ( type-name )
    > _FuncRetType identifier
    >
    > Where 'identifier' would have to have been declared as a function type.
    >
    > Where the '_FuncRetType' operator would yield a 'type-name' that is the
    > same type as the return type of either:
    > - A function whose type is the 'type-name' operand
    > - The function designated by the 'identifier' operand

    [...]

    Rather than just an identifier, the second form should take an
    expression of function type. Or, for more flexibility, of either
    function type or pointer-to-function type.

    I can see that it might be useful; I have no particular opinion on
    whether it would be worth adding to the language.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 21, 2012
    #2
    1. Advertising

  3. Shao Miller

    Shao Miller Guest

    On 2/20/2012 19:48, Keith Thompson wrote:
    > Shao Miller<> writes:
    >> Would anyone use a '_FuncRetType' operator, described briefly, below?
    >>
    >> Where the 'type-name' syntax would be extended to include:
    >>
    >> type-name:
    >> ...
    >> _FuncRetType ( type-name )
    >> _FuncRetType identifier
    >>
    >> Where 'identifier' would have to have been declared as a function type.
    >>
    >> Where the '_FuncRetType' operator would yield a 'type-name' that is the
    >> same type as the return type of either:
    >> - A function whose type is the 'type-name' operand
    >> - The function designated by the 'identifier' operand

    > [...]
    >
    > Rather than just an identifier, the second form should take an
    > expression of function type.


    Good point!

    > Or, for more flexibility, of either
    > function type or pointer-to-function type.
    >


    Good point!

    > I can see that it might be useful; I have no particular opinion on
    > whether it would be worth adding to the language.
    >


    Thanks for the productive feed-back, Keith.

    P. S. I accidentally posted to the wrong news-group.
    Shao Miller, Feb 21, 2012
    #3
  4. Shao Miller <> writes:

    > Would anyone use a '_FuncRetType' operator, described briefly, below?
    >
    > Where the 'type-name' syntax would be extended to include:
    >
    > type-name:
    > ...
    > _FuncRetType ( type-name )
    > _FuncRetType identifier
    >
    > Where 'identifier' would have to have been declared as a function
    > type.


    I'd go further and permit a unary expression -- just like sizeof -- and
    call it _Typeof:

    #define SWAP(x,y) do { _Typeof x t = x; x = y; y = t; } while (0)

    > Where the '_FuncRetType' operator would yield a 'type-name' that is
    > the same type as the return type of either:
    > - A function whose type is the 'type-name' operand
    > - The function designated by the 'identifier' operand


    .... yes, but generalised to the type of an expression?

    <snip uses>
    --
    Ben.
    Ben Bacarisse, Feb 21, 2012
    #4
  5. Shao Miller

    gwowen Guest

    On Feb 21, 2:51 am, Ben Bacarisse <> wrote:

    > > Where the '_FuncRetType' operator would yield a 'type-name' that is
    > > the same type as the return type of either:
    > > - A function whose type is the 'type-name' operand
    > > - The function designated by the 'identifier' operand

    >
    > ... yes, but generalised to the type of an expression?


    How different is this from C++11's 'auto'?
    gwowen, Feb 21, 2012
    #5
  6. Shao Miller

    James Kuyper Guest

    On 02/21/2012 04:41 AM, gwowen wrote:
    > On Feb 21, 2:51�am, Ben Bacarisse <> wrote:
    >
    >>> Where the '_FuncRetType' operator would yield a 'type-name' that is
    >>> the same type as the return type of either:
    >>> - A function whose type is the 'type-name' operand
    >>> - The function designated by the 'identifier' operand

    >>
    >> ... yes, but generalised to the type of an expression?

    >
    > How different is this from C++11's 'auto'?


    'auto' is a type specifier, not a a type name. It can only be used in
    the definition of a variable with an initializer, or the declaration of
    a function with a trailing return type.
    --
    James Kuyper
    James Kuyper, Feb 21, 2012
    #6
  7. gwowen <> writes:

    > On Feb 21, 2:51 am, Ben Bacarisse <> wrote:
    >
    >> > Where the '_FuncRetType' operator would yield a 'type-name' that is
    >> > the same type as the return type of either:
    >> > - A function whose type is the 'type-name' operand
    >> > - The function designated by the 'identifier' operand

    >>
    >> ... yes, but generalised to the type of an expression?

    >
    > How different is this from C++11's 'auto'?


    I suspect many of the use cases are the same (my example, for instance),
    but you could not use auto to do what Shao Miller originally suggested.

    [C++'s auto has two uses -- obviously its use to allow trailing return
    types in function declarators is quite another matter.]

    --
    Ben.
    Ben Bacarisse, Feb 21, 2012
    #7
  8. Shao Miller

    gwowen Guest

    On Feb 21, 11:20 am, James Kuyper <> wrote:

    > 'auto' is a type specifier, not a a type name. It can only be used in
    > the definition of a variable with an initializer, or the declaration of
    > a function with a trailing return type.


    Ahh, I'd forgotten about decltype()- thought auto sufficed for
    typenames too.
    gwowen, Feb 22, 2012
    #8
  9. Shao Miller

    mt Guest

    On Feb 20, 7:29 pm, Shao Miller <> wrote:
    > Would anyone use a '_FuncRetType' operator, described briefly, below?
    >
    > Where the 'type-name' syntax would be extended to include:
    >
    >    type-name:
    >      ...
    >      _FuncRetType ( type-name )
    >      _FuncRetType identifier
    >
    > Where 'identifier' would have to have been declared as a function type.
    >
    > Where the '_FuncRetType' operator would yield a 'type-name' that is the
    > same type as the return type of either:
    > - A function whose type is the 'type-name' operand
    > - The function designated by the 'identifier' operand
    >
    > Then you might do:
    >
    >    /* Like 'int x;' */
    >    _FuncRetType (int(void)) x;
    >
    > or:
    >
    >    int foo(void);
    >    /* Like 'int x;' */
    >    _FuncRetType foo x;
    >
    > or:
    >
    >    typedef int f_int_no_args(void);
    >    /* Like 'int x;' */
    >    _FuncRetType(f_int_no_args) x;
    >
    > or:
    >
    >    /* Third-party-provided version1.h */
    >    /* Might be present as 'version.h' */
    >    int my_func(void);
    >
    >    /* Third-party-provided version2.h */
    >    /* Might be present as 'version.h' */
    >    long long int my_func(void);
    >
    >    /* user.c */
    >    #include "version.h"
    >
    >    typedef _FuncRetType my_func rt_my_func;
    >    /* Like 'int x;' */
    >    rt_my_func x;
    >
    >    x = my_func();


    Looks like functional programming.
    mt, Feb 23, 2012
    #9
  10. Shao Miller

    Shao Miller Guest

    On 2/20/2012 21:51, Ben Bacarisse wrote:
    > Shao Miller<> writes:
    >
    >> Would anyone use a '_FuncRetType' operator, described briefly, below?
    >>
    >> Where the 'type-name' syntax would be extended to include:
    >>
    >> type-name:
    >> ...
    >> _FuncRetType ( type-name )
    >> _FuncRetType identifier
    >>
    >> Where 'identifier' would have to have been declared as a function
    >> type.

    >
    > I'd go further and permit a unary expression -- just like sizeof --and
    > call it _Typeof:
    >


    Apparently it was rejected for insufficient utility.

    > #define SWAP(x,y) do { _Typeof x t = x; x = y; y = t; } while (0)
    >


    Or, for safety against multiple evaluations of 'x' and 'y':

    #define SWAP(x_, y_) \
    do { \
    _Typeof &(x_) tx = &(x_); \
    _Typeof &(y_) ty = &(y_); \
    _Typeof (x_) t = *tx; \
    *tx = *ty; \
    *ty = t; \
    } while (0)
    /* ... */
    while (xp < endp)
    SWAP(*xp++, *yp++);

    >> Where the '_FuncRetType' operator would yield a 'type-name' that is
    >> the same type as the return type of either:
    >> - A function whose type is the 'type-name' operand
    >> - The function designated by the 'identifier' operand

    >
    > ... yes, but generalised to the type of an expression?
    >
    > <snip uses>


    Good idea! So if we have:

    void foo(void) {
    int x = 42;

    { /* sub-block */
    _Typeof x * x = NULL;
    }
    return;
    }

    then '_Typeof x * x = NULL' would be akin to 'int * x = NULL' versus
    'int = NULL', whereas '_Typeof (x * x) = NULL' would be akin to 'int =
    NULL' and '_Typeof (x * x = NULL)' would be akin to 'int', both latter
    cases being erroneous.

    Then if we wanted the return-type of a function, it'd be as simple as
    '_Typeof func()', where 'func()' wouldn't be evaluated, perhaps, or
    enjoyed similar semantics as 'sizeof' and '_Alignof' regarding evaluation.

    I asked because while something like a '_Typeof' was apparently
    rejected, perhaps the utility of getting just a function's return-type
    could yield a lower cost in language-change...?
    Shao Miller, Feb 25, 2012
    #10
  11. Shao Miller <> writes:
    [...]
    > Then if we wanted the return-type of a function, it'd be as simple as
    > '_Typeof func()', where 'func()' wouldn't be evaluated, perhaps, or
    > enjoyed similar semantics as 'sizeof' and '_Alignof' regarding evaluation.

    [...]

    The only problem with that is that if func() takes arguments, you'd have
    to supply dummy arguments of the right type (unless there was a special
    rule for a function call that's the operand of _Typeof).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 25, 2012
    #11
    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. Dan
    Replies:
    5
    Views:
    530
  2. N3TB1N
    Replies:
    4
    Views:
    568
    Stephen Waits
    May 10, 2004
  3. gavino
    Replies:
    18
    Views:
    444
    Mark Westwood
    Dec 1, 2006
  4. =?ISO-8859-1?Q?=C9ric_Daigneault?=

    RE: why would anyone use python when java is there?

    =?ISO-8859-1?Q?=C9ric_Daigneault?=, Nov 29, 2006, in forum: Python
    Replies:
    1
    Views:
    299
    Antoine De Groote
    Nov 29, 2006
  5. Jake Barnes
    Replies:
    50
    Views:
    376
    Matej
    Feb 13, 2006
Loading...

Share This Page