Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Would Anyone Use a _FuncRetType Operator?

Reply
Thread Tools

Would Anyone Use a _FuncRetType Operator?

 
 
Shao Miller
Guest
Posts: n/a
 
      02-21-2012
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();
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      02-21-2012
Shao Miller <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      02-21-2012
On 2/20/2012 19:48, Keith Thompson wrote:
> Shao Miller<(E-Mail Removed)> 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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-21-2012
Shao Miller <(E-Mail Removed)> 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.
 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      02-21-2012
On Feb 21, 2:51*am, Ben Bacarisse <(E-Mail Removed)> 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'?
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-21-2012
On 02/21/2012 04:41 AM, gwowen wrote:
> On Feb 21, 2:51´┐Żam, Ben Bacarisse <(E-Mail Removed)> 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-21-2012
gwowen <(E-Mail Removed)> writes:

> On Feb 21, 2:51┬*am, Ben Bacarisse <(E-Mail Removed)> 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.
 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      02-22-2012
On Feb 21, 11:20*am, James Kuyper <(E-Mail Removed)> 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.
 
Reply With Quote
 
mt
Guest
Posts: n/a
 
      02-23-2012
On Feb 20, 7:29*pm, Shao Miller <(E-Mail Removed)> 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.
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      02-25-2012
On 2/20/2012 21:51, Ben Bacarisse wrote:
> Shao Miller<(E-Mail Removed)> 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...?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
an oddball scary kind of thing you would think would never happen richard Computer Support 4 01-31-2010 06:34 PM
why would anyone use python when java is there? gavino Python 18 12-01-2006 09:06 AM
RE: why would anyone use python when java is there? =?ISO-8859-1?Q?=C9ric_Daigneault?= Python 1 11-29-2006 04:47 PM
if I wanted to never use innerHTML, what else would I use? Jake Barnes Javascript 50 02-13-2006 07:34 PM
Anyone know why a page setup in IE that is the same on 2 comps would look different?? Dan ASP .Net 5 01-05-2006 10:17 AM



Advertisments