function overloading

L

lordkain

is it possible to do some kind of function overloading in c? and that
the return type is different
 
R

Richard Heathfield

(e-mail address removed) said:
is it possible to do some kind of function overloading in c? and that
the return type is different

Function overloading is not *supported* in C. There is no syntax for it, so
to speak. Whether it is *possible* is another question. I would answer an
unequivocal "no", were it not for the fact that comp.lang.c subscribers
frequently surprise me with their ingenuity! Nevertheless, any solution is
likely to be so contorted that you may find it better to find another way
around your problem.

I may be mistaken here, but I have a hunch that you have not asked the
question you perhaps ought to be asking. It seems to me that there is some
task (call it X) that you need to achieve, and you've thought about it, and
decided that function overloading would be a way to achieve that task, and
so you're asking about function overloading, whereas you may find it more
productive to ask about ways in which X itself, whatever it may be, can be
achieved.

If any practical programming language is Turing-complete, so is C. (And if
none is, C fails in that regard only in the same way that other languages
do.) Therefore, if it's computable, C can compute it. That does not,
however, mean "if some other language has feature F, then C must have
feature F too". Different languages provide different ways of getting from
a problem to a solution. Just because C doesn't support function
overloading, that doesn't mean you can't solve your /real/ problem in C.

Incidentally, you hint that you wish to be able to distinguish between
functions of the same name purely by virtue of their having different
return types. Even C++ (which *does* support function overloading) cannot
do this.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
C

Chris Dollin

Richard said:
Incidentally, you hint that you wish to be able to distinguish between
functions of the same name purely by virtue of their having different
return types. Even C++ (which *does* support function overloading) cannot
do this.

There are languages which do. I /think/ Ada is one of them. But this
is a fuzzy memory from -- ye gods and little finches -- more than 20
years ago.
 
J

Jean-Marc Bourguet

Chris Dollin said:
There are languages which do. I /think/ Ada is one of them. But this
is a fuzzy memory from -- ye gods and little finches -- more than 20
years ago.

Ada is able to do it.

C++ is not able to do it but is able to somewhat fake it. The way is to
return a proxy which and do all the hard work user conversion operators.

A+
 
G

Guest

Richard Heathfield wrote:> Incidentally, you hint that you wish to be
able to distinguish between > functions of the same name purely by
virtue of their having different > return types. Even C++ (which *does*
support function overloading) cannot > do this.<OT>Yes, it can, in
limited cases. An example (of poor C++-style):#include
<stdio.h>namespace A { void foo() { puts("void foo()"); }}namespace B
{ int foo() { puts("int foo()"); return 0; }}using A::foo;using
B::foo;int main() { ((void(*)()) foo)(); ((int (*)()) foo)();}</OT>
 
K

KISHORE

is it possible to do some kind of function overloading in c? and that
the return type is different


Since If we observe The limitations of c,and list out them we can
conclude that function overloading cannot be done in c language.thats
why there r seperate functions for different types in <math.h> to
clarify ur doubt open turboc editor and open math.h header file and
gothrough the function declerations.u can know the answer.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

KISHORE said:
Since If we observe The limitations of c,and list out them we can
conclude that function overloading cannot be done in c language.thats
why there r seperate functions for different types in <math.h> to
clarify ur doubt open turboc editor and open math.h header file and
gothrough the function declerations.u can know the answer.

Unless the files that comes with TurboC are crafted to work with
TurboC only, and are not really standard C.
(substitute TurboC with whatever random implementation on your hand)
 
C

CBFalconer

Harald said:
Richard Heathfield wrote:> Incidentally, you hint that you wish to be
able to distinguish between > functions of the same name purely by
virtue of their having different > return types. Even C++ (which *does*
support function overloading) cannot > do this.<OT>Yes, it can, in
limited cases. An example (of poor C++-style):#include
<stdio.h>namespace A { void foo() { puts("void foo()"); }}namespace B
{ int foo() { puts("int foo()"); return 0; }}using A::foo;using
B::foo;int main() { ((void(*)()) foo)(); ((int (*)()) foo)();}</OT>

A shining example of why not to use the faulty google interface to
usenet.
 
R

Richard Heathfield

[Broken flow fixed]

Harald van D?k said:
<OT>Yes, it can,

No, it can't.
in limited cases. An example (of poor C++-style):

#include <stdio.h>
namespace A { void foo(){puts("void foo()");}}
namespace B { int foo(){puts("int foo()"); return 0;}}
using A::foo;using B::foo;
int main() {((void(*)()) foo)(); ((int (*)()) foo)();}
</OT>

Your example does not distinguish between functions of the same name purely
by virtue of their having different return types. It also requires them to
be in different namespaces, and thus the return type is not the only
distinguishing characteristic.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
S

Stephen Sprunk

is it possible to do some kind of function overloading in c? and that
the return type is different

In general, no; C does not support overloading as part of the language.
However, if you have a C99 system, take a look at how <tgmath.h> is
implemented. Depending on what you're doing, something similar may
work, but you won't end up with portable code. In general, it's better
to find a solution that doesn't require overloading in the first place.

My implementation does something similar to this:

int foo_char(char x);
int foo_int(int x);
#define foo(x) { if (sizeof(x)==sizeof(char)) foo_char(x); else
foo_int(x); }

This, of course, assumes that all of the possible arguments to your
"overloaded" function can be distinguished by size; that's not a
portable assumption. Too bad there's no standard "typeof" operator.

S
 
K

Keith Thompson

CBFalconer said:
A shining example of why not to use the faulty google interface to
usenet.

Since I've never seen this particular problem in a Google post, it's
not clear that Google is at fault.
 
T

Tor Rustad

Stephen Sprunk skrev:

[...]
My implementation does something similar to this:

int foo_char(char x);
int foo_int(int x);
#define foo(x) { if (sizeof(x)==sizeof(char)) foo_char(x); else
foo_int(x); }

You should consider using the macro

#define foo(x) ((sizeof((x))==sizeof(char) ? (foo_char((x))) :
(foo_int)((x)))

instead. However, this will not work in cases like

foo( foo(x) )

so I guess most C99 implementations use compiler magic

#define foo(x) __foo_generic((x), foo_char, foo_int)
 
G

Guest

Keith said:
Since I've never seen this particular problem in a Google post, it's
not clear that Google is at fault.

I would've commented sooner, but I didn't want to mess up again. I'm
not sure what happened myself. In Google's preview, my message appeared
okay, but after posting, well, you saw what happened. The only thing I
did differently was using w3m instead of konqueror. I'll avoid using
that to post in the future, so hopefully it'll not happen again.
 
L

lordkain

Hi Richard,
Richard Heathfield schreef:
I may be mistaken here, but I have a hunch that you have not asked the
question you perhaps ought to be asking. It seems to me that there is some
task (call it X) that you need to achieve, and you've thought about it, and
decided that function overloading would be a way to achieve that task, and
so you're asking about function overloading, whereas you may find it more
productive to ask about ways in which X itself, whatever it may be, can be
achieved.

You're absolutly right. Im trying to do a task called X and figured out
that the best option could be function overloading. My real task and
burning question that I'm bound to solve is to be found at
http://groups.google.nl/group/comp....466a1/ef4da1f26611bb7a?hl=nl#ef4da1f26611bb7a

or look at the subject [xml to function call] in the google newsgroup.

Cheerz, David
 
R

Richard Heathfield

(e-mail address removed) said:
or look at the subject [xml to function call] in the google newsgroup.

It looks to me like you just need to write an XML-to-C translator for
building your test harness. I don't see any need for function overloading.
Eric S seems to have given you some good tips.
 
S

Stephen Sprunk

Tor Rustad said:
Stephen Sprunk skrev:

You should consider using the macro

#define foo(x) ((sizeof((x))==sizeof(char) ? (foo_char((x))) :
(foo_int)((x)))

instead. However, this will not work in cases like

foo( foo(x) )

so I guess most C99 implementations use compiler magic

#define foo(x) __foo_generic((x), foo_char, foo_int)

My example was from the Linux/GCC/Glibc headers on my system. I did
paraphrase a bit, to make things clearer since there were multiple
levels of macros obscuring things, but that's pretty much what one will
end up with after preprocessing.

I'm curious what the failure modes are, if any, with that construction.

S
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top