Would Anyone Use a _FuncRetType Operator?

S

Shao Miller

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();
 
K

Keith Thompson

Shao Miller said:
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.
 
S

Shao Miller

Shao Miller said:
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.
 
B

Ben Bacarisse

Shao Miller said:
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>
 
J

James Kuyper

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.
 
B

Ben Bacarisse

gwowen said:
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.]
 
G

gwowen

'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.
 
M

mt

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.
 
S

Shao Miller

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++);
... 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...?
 
K

Keith Thompson

Shao Miller said:
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).
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top