const function() !!??

A

Andrey Tarasevich

dandelion said:
...
const int get_foobar(int i)
{
assert(i<3);
return foobar;
}
...


And how and when is this better than just

int get_foobar(int i)

with the same body?


It's not much better anytime, anywhere. But hey... I was just looking for a
way (any way) that would make returning a const int a bit more sensible.
Best it could *possibly* do is to provide a hint to the programmer that
modifying the result of get_foobar(int i) may yield undesired results.
...


But there's no way to even attempt to modify the result of 'get_foobar'
in C language! There's no need for any hint, for that reason.
 
D

dandelion

How would the programmer modify the result of get_foobar()?

Like you would any other integer. Assign it to ordinary 'int' and do
whatever it
is you want to do.

<example code>
#include <stdlib.h>
#include <stdio.h>

const int foo(void)
{
return 1;
}

int main(void)
{
int x;
x = foo() + 3;
x++;

x = 2*x + 1;

printf("x = %d\n", x);

return EXIT_SUCCESS;
}
(Incidentally, you'd also want an "assert(i>=0);".)

True, if this were production code.

I left it out since it's demonstration code, written to make a point which
wasn't about the usefullness of 'assert()'. I also omitted any #defines for
FOOBARx.
 
D

dandelion

this simply means that u don hve the concept of cost qualifier...

As has been pointed out by several people to several other people, it is
handy to include at least a
snippet of the post you are responding to in order to make it easyer for
other people to ascertain what the [BEEEP] you are talking about.
 
D

dandelion

Andrey Tarasevich said:
But there's no way to even attempt to modify the result of 'get_foobar'
in C language!

Ummm... Not sure what is meant here.
There's no need for any hint, for that reason.

Tastes differ. I did not say the code as presented is particularly usefull,
but still,
It's within the standard.
 
A

Andrey Tarasevich

dandelion said:
...

Ummm... Not sure what is meant here.
...

Return values of functions in C are rvalues. It is not possible to
modify an rvalue. In C there no way to even attempt to modify an rvalue.
The notion of "modification" is not applicable to rvalues at all.
There's no need to protect rvalues from modifications by using 'const',
because the fact that they are rvalues already protects them better than
any 'const' will ever be able to. That's what I meant.
 
K

Keith Thompson

dandelion said:
Like you would any other integer. Assign it to ordinary 'int' and do
whatever it is you want to do.

<example code>
[snip]

No, that's not modifying the result, it's modifying the value of a
*copy* of the result.

The function in question has been lost in snippage; it looked
something like this:

const int get_foobar(int i)
{
return blah;
}

Declaring the function to return a "const int" does not imply that the
caller shouldn't modify the value of a copy of the result. Indeed, it
would almost certainly be absurd to suggest such a thing. The copy is
the caller's own variable, no longer associated iwth the function in
any way, and the caller can do whatever it likes with it.

C provides no mechanism to even *attempt* to modify the actual result
of a function, any more than it allows you to modify a constant such
as 42. A function result is an rvalue.

An attempt to modify the function result might look like this:

get_foobar(2) = 10; /* illegal */

but of course that's not valid C, any more than

42 = 10; /* illegal */

is.

C allows you to declare that a function returns a const result, but
it's completely useless to do so, even as a hint to the programmer.

(Conceivably you could add a "const" to a function declaration to
force a pointer to that function to be incompatible with some other
function pointer, but that's ugly, and I'm not sure you can depend on
the compiler to enforce it.)
 
L

lawrence.jones

Andrey Tarasevich said:
Return values of functions in C are rvalues. It is not possible to
modify an rvalue. In C there no way to even attempt to modify an rvalue.

There is in C99 as I explained earlier in this thread. It's horribly
obscure (the rvalue in question must be a struct containing an array)
and it (deservedly) results in undefined behavior if you do it, but it
is possible to attempt it.

-Larry Jones

We don't ATTEND parties, we just CRASH 'em. -- Calvin
 

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
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top