const function() !!??

S

Sundar

Do we have something called a const function ? I thought applying the
'const' specifier to a function is meaningless... but when i tried
defining a const function it worked !!
like...
const int func(int a)
{
int b;
.....
return b;
}

...The above code gave no compilation errors ! :->
How should i interpret it ?
(i used MS- vc++ compiler)

Sundar
 
E

Emmanuel Delahaye

Sundar wrote on 30/12/04 :
Do we have something called a const function ? I thought applying the
No.

'const' specifier to a function is meaningless...
defining a const function it worked !!
like...
const int func(int a)
{
int b;
.....
return b;
}

That 'const' is useless.

This one is useful:

char const *get_ver (void)
{
return "1.2";
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
A

Andrey Tarasevich

Sundar said:
Do we have something called a const function ? I thought applying the
'const' specifier to a function is meaningless...

You can't apply the 'const' specifier to a function in C. That would be
a syntax error.
but when i tried
defining a const function it worked !!
like...
const int func(int a)
{
int b;
.....
return b;
}

This is not a 'const' function. In accordance with C syntax, this
'const' qualifier is applied to the return type of the function. I.e.
this function return type is 'const int'.

Syntactically, a naive attempt to apply a 'const' qualifier to a
function would look as follows

int (const func)(int a)

but this simply won't compile.
 
K

Keith Thompson

dandelion said:
And it shouldn't.


A function returning a const int, that is an non-modifyable integer.

But you can't modify the result of a function anyway.

Is there any context in which there's a difference between
const int func(int a) { /* blah blah */ }
and
int func(int a) { /* blah blah */ }
?

As far as I can tell there isn't, and gcc warns:

tmp.c:3: warning: type qualifiers ignored on function return type

I suppose pointers to the two function types would be incompatible,
but I can't think of any other difference.

Probably it was just easier to allow it in the language definition,
even though it's meaningless, than to disallow it.
 
M

__MMS__

Thanks !
I understand 'const' function is meaningless, but please tell me why to
use a 'const' as return type ?!
 
R

Raymond Martineau

Thanks !
I understand 'const' function is meaningless, but please tell me why to
use a 'const' as return type ?!

Because the return value of the function would result in undefined
behaviour if it were changed (e.g. returning a string literal).
 
S

Stephen Sprunk

__MMS__ said:
Thanks !
I understand 'const' function is meaningless, but please tell me why to
use a 'const' as return type ?!

You may want to return a const pointer because you don't want the pointed-to
object to be changed.

S
 
K

Keith Thompson

Stephen Sprunk said:
You may want to return a const pointer because you don't want the pointed-to
object to be changed.

That's a good reason for returning a pointer-to-const-whatever, but I
think the OP was asking why you would want to return a const-whatever
(where "whatever" itself may or may not be a pointer type). As far as
I can tell, there's no reason to do so.
 
A

Andrey Tarasevich

__MMS__ said:
...
I understand 'const' function is meaningless, but please tell me why to
use a 'const' as return type ?!
...

There's absolutely no point in doing it in C. Values returned by
functions are rvalues. Const-qualification has no effect on rvalues in C
(in C++ things are different, but that's offtopic here).
 
D

dandelion

Keith Thompson said:
That's a good reason for returning a pointer-to-const-whatever, but I
think the OP was asking why you would want to return a const-whatever
(where "whatever" itself may or may not be a pointer type). As far as
I can tell, there's no reason to do so.

const int foobar[] =
{
FOOBAR1, /* Some config data */
FOOBAR2,
FOOBAR3
};

const int get_foobar(int i)
{
assert(i<3);
return foobar;
}
 
A

Andrey Tarasevich

dandelion said:
...
That's a good reason for returning a pointer-to-const-whatever, but I
think the OP was asking why you would want to return a const-whatever
(where "whatever" itself may or may not be a pointer type). As far as
I can tell, there's no reason to do so.

const int foobar[] =
{
FOOBAR1, /* Some config data */
FOOBAR2,
FOOBAR3
};

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

Keith Thompson

Sundar said:
Agree with you Keith :)

Thanks.

It's a good idea to provide some context, so readers can know what
you're referring to without going back to the parent article (which
may be difficult in some newsreaders). Since you didn't provide any
context, I'll just assume you agree with everything I say -- which I
must say is a remarkably enlightened attitude.

In case anyone is interested, the context was:

] That's a good reason for returning a pointer-to-const-whatever, but I
] think the OP was asking why you would want to return a const-whatever
] (where "whatever" itself may or may not be a pointer type). As far as
] I can tell, there's no reason to do so.
 
L

Lawrence Kirby

Because the return value of the function would result in undefined
behaviour if it were changed (e.g. returning a string literal).

It is simply not possible in C to change a value such as the return value
of a function (*). All you can do is operate on it to generate a new value.

In the case of a string literal you a pointer to const char to do what you
are saying, but the pointer itself isn't const.

Syntactically you can qualify the return type of a function but it serves
no purpose. A more evil thing is to define a const function which cna be
done using typedef e.g.

typedef void func(void);
typedef const func constfunc;

However the standard says that this has undefined behaviour.

* - there are some issues arising from being able to return a structure or
union type but nothing very interesting in this respect.

Lawrence
 
D

dandelion

Andrey Tarasevich said:
dandelion said:
...
That's a good reason for returning a pointer-to-const-whatever, but I
think the OP was asking why you would want to return a const-whatever
(where "whatever" itself may or may not be a pointer type). As far as
I can tell, there's no reason to do so.

const int foobar[] =
{
FOOBAR1, /* Some config data */
FOOBAR2,
FOOBAR3
};

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.

Otherwise the two are equivalent in all respects (as you know).
 
K

Keith Thompson

dandelion said:
Andrey Tarasevich said:
dandelion said:
...
That's a good reason for returning a pointer-to-const-whatever, but I
think the OP was asking why you would want to return a const-whatever
(where "whatever" itself may or may not be a pointer type). As far as
I can tell, there's no reason to do so.

const int foobar[] =
{
FOOBAR1, /* Some config data */
FOOBAR2,
FOOBAR3
};

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.

Otherwise the two are equivalent in all respects (as you know).


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

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

lawrence.jones

Lawrence Kirby said:
* - there are some issues arising from being able to return a structure or
union type but nothing very interesting in this respect.

Well, they're interesting in that they are the only way that you can
even attempt to modify the return value of a function, but you get
undefined behavior if you do.

-Larry Jones

Some people just don't have inquisitive minds. -- Calvin
 
V

Villy Kruse

Well, they're interesting in that they are the only way that you can
even attempt to modify the return value of a function, but you get
undefined behavior if you do.

How would you actualy do that? Can you take the address of such a
returned structure and assign it to an appropriate pointer, and use that
pointer to modify the value? If you assign the returned structure to
another structure variable then the other scruture becomes a copy of the
returned sctructer, doesn't it?

Villy
 
L

lawrence.jones

Villy Kruse said:
How would you actualy do that? Can you take the address of such a
returned structure and assign it to an appropriate pointer, and use that
pointer to modify the value?

No, the structure is not an lvalue so you can't take its address. The
structure must contain an array -- in order to allow you to reference an
element of such an array, the array is converted into a pointer to its
first element *even when the array is not an lvalue*. So somthing like:

f().a[0] = 6;

violates no constraints and attempts to modify the function return value
(which results in undefined behavior).

-Larry Jones

I always have to help Dad establish the proper context. -- 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