473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2731
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
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
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
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
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
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
10
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.