[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

const int return type

Cocon Cocon

8/4/2011 12:07:00 PM

I see what 'const' does to global variables and pointer arguments. But
how about return types?

Consider:

const int f() {
...
}

What does the 'const' change?

Specifically, what would one have to put in for the '...' in order to
get a compile-time error?
2 Answers

Ben Bacarisse

8/4/2011 1:25:00 PM

0

Cocon Cocon <comuesli@gmail.com> writes:

> I see what 'const' does to global variables and pointer arguments. But
> how about return types?
>
> Consider:
>
> const int f() {
> ...
> }
>
> What does the 'const' change?

Nothing. const is a property of objects not values and in C functions
return values not objects. That's a simplification -- a more technical
description is that function calls are not lvalue expressions.

> Specifically, what would one have to put in for the '...' in order to
> get a compile-time error?

I'd have thought you would be likely to get a diagnostic just for having
the const there though I don't think the C standard requires one.

--
Ben.

Keith Thompson

8/4/2011 2:48:00 PM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Cocon Cocon <comuesli@gmail.com> writes:
>> I see what 'const' does to global variables and pointer arguments. But
>> how about return types?
>>
>> Consider:
>>
>> const int f() {
>> ...
>> }
>>
>> What does the 'const' change?
>
> Nothing. const is a property of objects not values and in C functions
> return values not objects. That's a simplification -- a more technical
> description is that function calls are not lvalue expressions.
>
>> Specifically, what would one have to put in for the '...' in order to
>> get a compile-time error?
>
> I'd have thought you would be likely to get a diagnostic just for having
> the const there though I don't think the C standard requires one.

It doesn't, but gcc (with the right options) warns:

warning: type qualifiers ignored on function return type

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"