f



type declaration in declaration of a parameter or return type of a function

hello

 is it strictly conforming to declare a type (struct, union or enum)
in the declaration of a parameter
or of the return type of a function?
For instance:
  struct s { int a; float b; } f( int ) ;
  int g ( enum e { zero, one, two } x ) { enum e y;  return x==y; }
0
Luca
11/12/2010 6:53:36 PM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

4 Replies
908 Views

Similar Articles

[PageSpeed] 40

Luca Forlizzi wrote:

> hello
> 
>  is it strictly conforming to declare a type (struct, union or enum)
> in the declaration of a parameter
> or of the return type of a function?
> For instance:
>   struct s { int a; float b; } f( int ) ;
>   int g ( enum e { zero, one, two } x ) { enum e y;  return x==y; }

Yes it is strictly conforming. It should be noted that in the struct/union 
case, you should better not include a tag. If you do, you cannot call the 
function without invoking undefined behavior across translation units.

In any case with or without a tag, (in the struct/union case) you cannot 
call the function from the same translation unit, except when called 
directly recursively. And it is not possible for the struct/union case to 
split the definition and declaration, when both of them appear in the same 
translation unit. For the enum case, you cannot split it if the compiler 
decides the underlying type "compatible type" used for the enumeration is 
different for the two eumeration specifiers. 

I could be wrong on this thoug, I'm only talking from what I recall.
0
Johannes
11/12/2010 7:27:46 PM
On 12 Nov, 20:27, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> Luca Forlizzi wrote:
> > hello
>
> > =A0is it strictly conforming to declare a type (struct, union or enum)
> > in the declaration of a parameter
> > or of the return type of a function?
> > For instance:
> > =A0 struct s { int a; float b; } f( int ) ;
> > =A0 int g ( enum e { zero, one, two } x ) { enum e y; =A0return x=3D=3D=
y; }
>
> Yes it is strictly conforming. It should be noted that in the struct/unio=
n
> case, you should better not include a tag. If you do, you cannot call the
> function without invoking undefined behavior across translation units.
>
> In any case with or without a tag, (in the struct/union case) you cannot
> call the function from the same translation unit, except when called
> directly recursively. And it is not possible for the struct/union case to
> split the definition and declaration, when both of them appear in the sam=
e
> translation unit. For the enum case, you cannot split it if the compiler
> decides the underlying type "compatible type" used for the enumeration is
> different for the two eumeration specifiers.
>
> I could be wrong on this thoug, I'm only talking from what I recall.

I think you are right when the new type is declared inside a parameter
declaration, because the scope of the declared type is the function
body (in case of a function definition).
However, if a new type is declared ad return type of the function:
 if the type is declared just in the first declaration of the
function, everything is normal because the new type has file scope;
 if the type is declared also in the following declaration of the
function it is a constraint violation.

Would you agree?
0
Luca
11/13/2010 5:44:30 PM
Luca Forlizzi wrote:

> On 12 Nov, 20:27, "Johannes Schaub (litb)" <schaub-johan...@web.de>
> wrote:
>> Luca Forlizzi wrote:
>> > hello
>>
>> > is it strictly conforming to declare a type (struct, union or enum)
>> > in the declaration of a parameter
>> > or of the return type of a function?
>> > For instance:
>> > struct s { int a; float b; } f( int ) ;
>> > int g ( enum e { zero, one, two } x ) { enum e y;  return x==y; }
>>
>> Yes it is strictly conforming. It should be noted that in the
>> struct/union case, you should better not include a tag. If you do, you
>> cannot call the function without invoking undefined behavior across
>> translation units.
>>
>> In any case with or without a tag, (in the struct/union case) you cannot
>> call the function from the same translation unit, except when called
>> directly recursively. And it is not possible for the struct/union case to
>> split the definition and declaration, when both of them appear in the
>> same translation unit. For the enum case, you cannot split it if the
>> compiler decides the underlying type "compatible type" used for the
>> enumeration is different for the two eumeration specifiers.
>>
>> I could be wrong on this thoug, I'm only talking from what I recall.
> 
> I think you are right when the new type is declared inside a parameter
> declaration, because the scope of the declared type is the function
> body (in case of a function definition).
> However, if a new type is declared ad return type of the function:
>  if the type is declared just in the first declaration of the
> function, everything is normal because the new type has file scope;
>  if the type is declared also in the following declaration of the
> function it is a constraint violation.
> 
> Would you agree?

Yes, I should have limited my sayings to the parameter type. 
0
Johannes
11/13/2010 6:08:01 PM
re-reading again your precious answer, I don't understand (and would
like to) the following:

On 12 Nov, 20:27, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> Luca Forlizzi wrote:
> > hello
>
> > =A0is it strictly conforming to declare a type (struct, union or enum)
> > in the declaration of a parameter
> > or of the return type of a function?
> > For instance:
> > =A0 struct s { int a; float b; } f( int ) ;
> > =A0 int g ( enum e { zero, one, two } x ) { enum e y; =A0return x=3D=3D=
y; }
>
> Yes it is strictly conforming. It should be noted that in the struct/unio=
n
> case, you should better not include a tag. If you do, you cannot call the
> function without invoking undefined behavior across translation units.

why is it so? If in another translation unit I declare a compatible
struct/union type with identical tag and identical members, where is
the problem?
Reading sec. 6.2.7 I can see no difference between the case of two
structs/unions both without a tag and the case where they have
identical tags.


About the case of a type declaration in the return type of a function,
I remark that there is a "forbidden" case similar to the ones you
pointed out: if a struct/union type is declared in the type specifier
of the function definition, it should have a tag, otherwise you can't
declare any other object of that type so it is not possible to have a
return statement in the function body. I think it's valid not to have
a return in the function body, but the return value is indeterminate
and reading it produces undef. behavior.
0
Luca
11/14/2010 9:30:29 PM
Reply: