By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,917 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,917 IT Pros & Developers. It's quick & easy.

function overloading

P: n/a
is it possible to do some kind of function overloading in c? and that
the return type is different

Nov 17 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
lo******@tlen.pl 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.
Nov 17 '06 #2

P: n/a
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.
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.

--
Chris "hantwig efferko VOOM!" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/

Nov 17 '06 #3

P: n/a
Chris Dollin <ch**********@hp.comwrites:
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.

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+

--
Jean-Marc
Nov 17 '06 #4

P: n/a
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>

Nov 17 '06 #5

P: n/a

lo******@tlen.pl wrote:
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.hto
clarify ur doubt open turboc editor and open math.h header file and
gothrough the function declerations.u can know the answer.

Nov 17 '06 #6

P: n/a
KISHORE wrote:
lo******@tlen.pl wrote:
>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.hto
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)
Nov 17 '06 #7

P: n/a
[Broken flow fixed]

Harald van D?k 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,
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.
Nov 17 '06 #8

P: n/a
Harald van D?k wrote:
>
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Nov 17 '06 #9

P: n/a
<lo******@tlen.plwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
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.his
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

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Nov 17 '06 #10

P: n/a
CBFalconer <cb********@yahoo.comwrites:
Harald van D?k wrote:
>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.
Since I've never seen this particular problem in a Google post, it's
not clear that Google is at fault.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 17 '06 #11

P: n/a
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)

--
Tor <torust AT online DOT no>

Nov 17 '06 #12

P: n/a
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
Harald van D?k wrote:
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.

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.

Nov 17 '06 #13

P: n/a
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.l...4da1f26611bb7a

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

Cheerz, David

Nov 23 '06 #14

P: n/a
lo******@tlen.pl 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 23 '06 #15

P: n/a
"Tor Rustad" <to********@hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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)
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

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Nov 23 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.