const int return type

C

Cocon Cocon

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

Ben Bacarisse

Cocon Cocon said:
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.
 
K

Keith Thompson

Ben Bacarisse said:
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.


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
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top