473,513 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function overloading

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

Nov 17 '06 #1
15 2737
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
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
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
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

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
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
[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
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
<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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2159
by: Doran_Dermot | last post by:
Hi All, I've seen lots of code in which the attributes of a class are accessed and modified using two separate methods. For example: class Problems: def __init__( self, refNum ):...
4
6448
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
16
16202
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
39
2192
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
3219
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
7
6104
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
10
2157
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
10
3470
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
14
1618
by: mesut | last post by:
hi colleagues, I don't know if this is the right group for but it's in C# so I try. I have a #3 procedural function called GetInfo.. and those are 3 overloaded methods. I would like to use the...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7162
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7527
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5090
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.