overloaded functions in C

B

bahadir.balban

Hi,

What's the best way to implement an overloaded function in C? For
instance if you want to have generic print function for various
structures, my implementation would be with a case statement:

void print_structs(void * struct_argument, enum struct_type stype)
{
switch(stype) {
case STRUCT_TYPE_1:
struct type1 * p = (struct type1 *) struct_argument;
printf("type1.field1: %d\n", type1.field1);

case STRUCT_TYPE_2:
...
}
return;
}

One thing I don't like about this is that, you must encode type
information
in an enum. I don't like this because I don't prefer encoding
user-defined aspects into code in general. Is there a better way of
doing this?

Thanks,
Bahadir
 
J

jacob navia

Hi,

What's the best way to implement an overloaded function in C? For
instance if you want to have generic print function for various
structures, my implementation would be with a case statement:

void print_structs(void * struct_argument, enum struct_type stype)
{
switch(stype) {
case STRUCT_TYPE_1:
struct type1 * p = (struct type1 *) struct_argument;
printf("type1.field1: %d\n", type1.field1);

case STRUCT_TYPE_2:
...
}
return;
}

One thing I don't like about this is that, you must encode type
information
in an enum. I don't like this because I don't prefer encoding
user-defined aspects into code in general. Is there a better way of
doing this?

Thanks,
Bahadir
lcc-win32 is a C compiler that implements overloaded functions.
http://www.cs.virginia.edu/~lcc-win32

Please:

flames >/dev/null
 
E

Eric Sosman

jacob said:
>
lcc-win32 is a C compiler that implements overloaded functions.

If it implements overloaded functions, it is not a C
compiler.
Please:

flames >/dev/null

I have not objected to your self-promotion before; every
useful signal carries some noise. But this latest post is the
worst I have seen: it is a deliberate attempt to mislead. It is
not merely advertising, but false advertising.

I will send my future flames to /dev/null if you will send
your future nonsense there, too. Deal?
 
J

jacob navia

Eric said:
If it implements overloaded functions, it is not a C
compiler.


The ANSI C standard page 7:A conforming implementation may have extensions (including additional
library functions), provided they do not alter the behavior of any
strictly conforming program.3)
And in the footnote 3 we have3) This implies that a conforming implementation reserves no identifiers
other than those explicitly reserved in this International Standard.
I have respected in the implementation of overloaded functions both
points.

So, even if you do not like it, lcc-win32 is a C compiler.
 
J

jacob navia

Richard said:
Phenomenally off-topic here, and you _know_ this, damn you, navia!

Since this newsgroup is too old to have a chart, this is your
personal appreciation. I beg to differ.

jacob
 
K

Kenny McCormack

Since this newsgroup is too old to have a chart, this is your
personal appreciation. I beg to differ.

jacob

Indeed. Well put.

I think that your (Jacob's) standard response to this kind of BS should be
to state:

"Asserting something 1,000 times does not, in and of itself, make it true."
 
L

Lawrence Kirby

On Thu, 09 Jun 2005 14:58:31 +0200, jacob navia wrote:

....
The ANSI C standard page 7:
A conforming implementation may have extensions (including additional
library functions), provided they do not alter the behavior of any
strictly conforming program.3)

And in the footnote 3 we have
3) This implies that a conforming implementation reserves no identifiers
other than those explicitly reserved in this International Standard.

I have respected in the implementation of overloaded functions both
points.

However these aren't the only requirements of a conforming implementation.
It must for example generate a diagnostic if the program contains a syntax
error or constraint violation. For example consider the constraint:

6.7p4 All declarations in the same scope that refer to the same object or
function shall specify compatible types.

Of course you can meet this requirement by generating diagnostics and
still compile the code, but it is usually easier (i.e. not to have to
explain diagnostics to your users) to support extensions like this in a
non-conforming mode.
So, even if you do not like it, lcc-win32 is a C compiler.

Most compilers can be invoked in various modes, some of which might be
conforming (or attempt to be) others not. So just saying that something is
a C compiler barely begins to tell the story.

Lawrence
 
J

jacob navia

Lawrence said:
Most compilers can be invoked in various modes, some of which might be
conforming (or attempt to be) others not. So just saying that something is
a C compiler barely begins to tell the story.

If you invoke lcc-win32 with the flag

-ansic

it will not accept any overloaded functions.
All other extensions aren't recognized either (operator
overloading and default function arguments).
 
C

Chris Croughton

If you invoke lcc-win32 with the flag

-ansic

it will not accept any overloaded functions.
All other extensions aren't recognized either (operator
overloading and default function arguments).

But in that mode it won't do what you claimed for it:
lcc-win32 is a C compiler that implements overloaded functions.

Either it is in a mode where it accepts overloaded functions, in which
case it is not a C compiler in that mode, or it is in -ansic mode where
it doesn't accept overloaded functions (in that mode it may or may not
be a C compiler for other reasons).

So saying "lcc-win32 is a C compiler that implements overloaded
functions" is false. It is a compiler which will compiler C /or/ this
other language with overloaded functions, but it will not do both with
the same program.

Since it is also pushing your own product, it is advertising.

The combination is "false advertising", which may be a crime in your
country (it is in others)...

Chris C
 
J

jacob navia

Chris said:
Either it is in a mode where it accepts overloaded functions, in which
case it is not a C compiler in that mode,

Chris C

Wrong. Please read the standard and see in page 7:
Extensions are not forbidden, if implemented according to the
constraints that I followed.

The -ansic flag disallows any extensions, and all
functions not in the standard. No windows.h no
network, etc etc.

Extensions are a common feature of most C compilers
and systems. The standard sets a minimum language,
but all implementation implement extensions in one way or
another.

Is gcc in non pedantic mode not a C compiler any more?

And Microsoft with their __declspec(...) is no longer C?

Let's be realistic and discuss the issues here.

A user asked about how to implement overloaded functions because
he needs them, they are useful. Most languages have this feature
in one way or another, from C# to C++ passing through all OO
languages, etc.

I understand C not as a freezed language that shuns any
improvement but as a language that should be as easy to
use as possible and that allows to construct modern software.

Overloaded functions allow more flexibility in the implementation
and less impact in the memory space of the user. Calling a
function
sqrt_for_long_doubles
sqrt_for_double
sqrt_for_float
just makes things more difficult to remember.

This is a plain truth, so much so, that the standard specifies
the overloaded function sqrt for all of those types.

That was a good idea, that I made available to the users of my
compiler system. Why the implementor should have something
that user's can't use? If the implementor uses them, they are
needed and the user have a right to use that too.

Calling all those three functions sqrt (as the standard header
tgmath defines) makes programming in C easier.

I just made that idea available to the users.

Let's stop the polemic and discuss the issues at hand. All of this
"I am holier than you" discussion is a waste of time.

Have you anything against genericity? Should be eliminate tgmath
from the standard then?

jacob
 
J

jacob navia

Lawrence said:
However these aren't the only requirements of a conforming implementation.
It must for example generate a diagnostic if the program contains a syntax
error or constraint violation. For example consider the constraint:

6.7p4 All declarations in the same scope that refer to the same object or
function shall specify compatible types.

You misunderstand overloaded functions.
suppose:

int overloaded fn(Mytype *arg);
int overloaded fn(MyOtherType *arg);

They are *not* the same object of course. They are TWO
different functions, that's the point!

The user has the facility of calling those two objects
with the same name with different argument signatures.

To stay within the standard, the overloaded function sqrt
refers to 3 different functions:
sqrtl(long double), sqrt(double) and sqrt(float).

This overloading doesn't mean that there is ONE sqrt function
but three of course.

My implementation will not accept:

int overloaded fn(Mytype *arg) {... }
int overloaded fn(Mytype *arg) {... }

because THAT would be a redefinition of the same object, as the
standard specifies!
 
C

CBFalconer

jacob said:
lcc-win32 is a C compiler that implements overloaded functions.
http://www.cs.virginia.edu/~lcc-win32

Instead of deliberately trolling with this you could have simply
said that this is a non-standard non-portable extension, available
in lcc-win32 for those who wish to fool with it. Is that so hard?
Then the clamor would probably be limited to pointing out the
insecurities it raises.
 
E

Eric Sosman

jacob said:
[...]
A user asked about how to implement overloaded functions because
he needs them, they are useful. [...]

The user asked, and I quote:
What's the best way to implement an overloaded function in C?

Look carefully at the end of the question. Squint your eyes,
and you might just be able to make out the requirement: "in C."
He didn't ask how to implement overloaded functions in Java or
Scheme or C++ or SmallTalk or in just any old language somebody
decides to suggest, he asked how to implement them "in C."

When you "answered" by suggesting he use a different language
(to wit, your own private language), you were unresponsive. When
you claimed that YOPL was in fact C, you were being untruthful.
Now, you're just being unpleasant.
 
C

Chris Croughton

Wrong. Please read the standard and see in page 7:

Extensions are not forbidden, if implemented according to the
constraints that I followed.

No, you did not, you implemented them in the user namespace.
The -ansic flag disallows any extensions, and all
functions not in the standard. No windows.h no
network, etc etc.

Not much use then, if it can't use external libraries and headers --
which /are/ allowed by the standard.
Extensions are a common feature of most C compilers
and systems. The standard sets a minimum language,
but all implementation implement extensions in one way or
another.

All? Well, perhaps you've checked every C compiler in existence.
Although extensions are necessary for some features of C99.
Is gcc in non pedantic mode not a C compiler any more?

Correct, it adds certain features (like asm) in the user namespace, thus
implementing a language almost, but not quite, C (indeed, with no
language specified the default is a language somewhere between C89 and
C99, with extra non-standard bits).
And Microsoft with their __declspec(...) is no longer C?

__declspec is in the implementation namespace, which is allowed by the
standard. Of course, they have had other inconsistencies as well...
Let's be realistic and discuss the issues here.

A user asked about how to implement overloaded functions because
he needs them, they are useful. Most languages have this feature
in one way or another, from C# to C++ passing through all OO
languages, etc.

And the answer is "They don't exist in C". If you want them, use
another language (C++ is a standard, for instance).
I understand C not as a freezed language that shuns any
improvement but as a language that should be as easy to
use as possible and that allows to construct modern software.

It certainly does the latter. If you want "as easy to use as possible"
I suspect you want LOGO or a form of BASIC.
Overloaded functions allow more flexibility in the implementation
and less impact in the memory space of the user. Calling a
function
sqrt_for_long_doubles
sqrt_for_double
sqrt_for_float
just makes things more difficult to remember.

Well, if those were the names they'd be easy to remember but hard to
type correctly. But fsqrt and lsqrt are pretty easy to remember,
particularly since they are consistent with the other maths functions'
naming. I would have liked to see dsqrt as a synonym for sqrt, just for
consistency, but I'm not too fussed about it.
This is a plain truth, so much so, that the standard specifies
the overloaded function sqrt for all of those types.

Only with the hideous tgmath.h extensions, which do indeed require
extensions (but not actually overloading, although they have that
effect).
That was a good idea, that I made available to the users of my
compiler system. Why the implementor should have something
that user's can't use? If the implementor uses them, they are
needed and the user have a right to use that too.

Indeed, I think that if they were going to allow it at all then it
should have been specified properly.

BUT IT WASN'T. That is the point, that no matter how much you want it
that feature IS NOT C.
Calling all those three functions sqrt (as the standard header
tgmath defines) makes programming in C easier.

I just made that idea available to the users.

And thereby created another language variant. It is not C, it is "C
with extensions".
Let's stop the polemic and discuss the issues at hand. All of this
"I am holier than you" discussion is a waste of time.

The issue is that a person asked whether overloaded functions are
available in C. The correct answer is simple, they are not. You,
however, attempted to mislead by pushing your almost-C compiler which
has them as extensions (and without pointing out that this is a
non-standard feature and is only available on one platform). That is
fraud,
Have you anything against genericity? Should be eliminate tgmath
from the standard then?

My preference is yes. It isn't needed (if you don't know what type you
want, your design is faulty, if it matters then you need to be able to
specify it). My next preference is that if they really think that
overloading is useful then it should be made generally available as a
standard feature.
 
S

S.Tobias

jacob navia said:
The ANSI C standard page 7:
[snip quotes]
I have respected in the implementation of overloaded functions both
points.
So, even if you do not like it, lcc-win32 is a C compiler.

1. Your compiler may be quite good and conforming (I don't know,
I haven't tried it, and it's not for me to judge anyway),
I think it is pretty clear that in this group particular
extensions are not topical. Otherwise valid topics would include
POSIX, Win32 API, __far pointers, __cdecl(export) declarations,
statement expressions, structured exceptions, __foos and __bars,
and many more in heaven and earth than are dreamt of in your
implementation.

2. You have not explained to the OP that C does not support
user defined function overloading, and in your implementation
it is an extension, and that it probably works _only_ in your
implementation. I don't think that inviting people to try
new language features in your product is bad by itself, but
I think that failing to mention that those features are
not portable - is.

3. All C++ compilers also support overloaded functions. So what?

4. OP asked a question how you *implement* overloaded functions (I
understand he meant the concept of overloaded functions).
In what way is your answer relevant?

5. Have you actually read the OP's article? Could you explain how
function overloading would solve his problem? Because from
what *I* have understood from his description, his needs are closer
to the concept of polymorphic types, rather than overloading.
 
J

jacob navia

Eric said:
jacob said:
[...]
A user asked about how to implement overloaded functions because
he needs them, they are useful. [...]


The user asked, and I quote:

What's the best way to implement an overloaded function in C?


Look carefully at the end of the question. Squint your eyes,
and you might just be able to make out the requirement: "in C."
He didn't ask how to implement overloaded functions in Java or
Scheme or C++ or SmallTalk or in just any old language somebody
decides to suggest, he asked how to implement them "in C."

When you "answered" by suggesting he use a different language
(to wit, your own private language), you were unresponsive. When
you claimed that YOPL was in fact C, you were being untruthful.
Now, you're just being unpleasant.

Typical of you Eric:

Just polemic, no arguments, no nothing.
"A different language" ???

Questions for you:

1) Is "sqrt" an overloaded function or not?
2) Isn't that defined in the C standard document?
3) What you thing about the definitions in tgmath.h?
("tg" stands for "Type Generic" math).
3) Why shouldn't this feature be available to all?

What do you have against this feature besides that
is an extension?
 
J

jacob navia

S.Tobias said:
5. Have you actually read the OP's article? Could you explain how
function overloading would solve his problem? Because from
what *I* have understood from his description, his needs are closer
to the concept of polymorphic types, rather than overloading.

Using overloaded functions the switch disappears and you have:

void *overloaded print_structs(Type1 *p)
{
printf("Struct Type 1":\n");
...
}


void *overloaded print_structs(Type2 *p)
{
printf("Struct Type 2":\n");
...
}

etc.

"Polymorphic types"... can you explain?

By the way, the C standard does have overloaded functions:

sqrt(double)
sqrt(long double)
sqrt(float)

are the same as

sqrtl(long double)
sqrt(double)
sqrtf(float)

the "function" sqrt is overloaded to 3 different functions.
And this is 100% standard C.

Why should this feature not be available to users?

That's the whole point here.

jacob
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top