473,378 Members | 1,420 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,378 software developers and data experts.

overloading in c

Hi,
I need a function like: config_write(key, value)

I am using gcc in linux.

But different keys associate with different types of values, ex. char,
long, char *,
One way is to provide different functions such as config_write_char(long
key, char value), config_write_buffer(long key, char *value)

But programmers will be happy if only one function is needed to write all
kinds of configuration items.

The following is my idea about this:

bool config_write(unsigned long key, ...)
{
va_list ap;
va_start(ap, key);
int a; char b; long c; double d; char *e;
switch(config_get_type(key)) {
case INT:
a = va_arg(ap, int);
...
break;
case BUFFER:
e = va_arg(ap, char *);
//process e;
break;
...
}
va_end(ap);
return TRUE;
}

Note: config_get_type(unsigned long key) is used to get the data type of a
key , enum will be returned. The data type of a configuration item will be
stored in a table.

But it has shortcomings: The compiler won't do type-checking for you. The
programmers need assure that the type of value they passed in is correct for
that config item

So I want to know how to use only one function: config_write, but still
let compiler do type-checking for me.

Thanks for your advice
Nov 15 '05 #1
3 1866
"Johnny" <lg***********@yahoo.com.cn> wrote:
I need a function like: config_write(key, value) But different keys associate with different types of values, ex. char,
long, char *,
One way is to provide different functions such as config_write_char(long
key, char value), config_write_buffer(long key, char *value) The following is my idea about this:

bool config_write(unsigned long key, ...) But it has shortcomings: The compiler won't do type-checking for you. The
programmers need assure that the type of value they passed in is correct for
that config item

So I want to know how to use only one function: config_write, but still
let compiler do type-checking for me.


You cannot, in C. If you really need this, go to C++; in most
circumstances, the solution you already give would be sufficient.

Richard
Nov 15 '05 #2
I don't have a solution for this problem, but I know
that gcc have some features that might come in handy.

It can do type checking of printf statements as long
as the format string is a constant. But that doesn't
seem general enough to apply in your case.

I tried to come up with something using typeof, a
bit of macro magic (including the # operator), nested
functions, and finally __PRETTY_FUNCTION__.
Unfortunately it seems __PRETTY_FUNCTION__ contains
only the necesarry information if you are using C++,
and there you don't have nested functions.

The closest I could come up with was a switch on
sizeof. But if two of the types you might be using
are of the same size, then that isn't of much help.
Here is the code. Can somebody suggest a fix? Or is
this really impossible?

#include <stdio.h>
#define config_write(key, value) do { \
char *__foobar (typeof(value) x) { \
return __PRETTY_FUNCTION__; \
} \
do_config_write(key,__foobar(value),value);\
} while(0)
void do_config_write(long key, char *type, ...)
{
printf("%ld: %s\n",key,type);
}
int main()
{
int foobar=42;
config_write(27,foobar);
return 0;
}

--
Kasper Dupont -- der bruger for meget tid på usenet.
Note to self: Don't try to allocate 256000 pages
with GFP_KERNEL on x86.
Nov 15 '05 #3
Kasper Dupont wrote:
I don't have a solution for this problem, but I know
that gcc have some features that might come in handy.

it can be found from gcc manual...

if (__builtin_types_compatible_p (typeof (x), int ))
my_int_func (x);
else if (__builtin_types_compatible_p (typeof (x), float ))
my_float_func (x);

It can do type checking of printf statements as long
as the format string is a constant. But that doesn't
seem general enough to apply in your case.

I tried to come up with something using typeof, a
bit of macro magic (including the # operator), nested
functions, and finally __PRETTY_FUNCTION__.
Unfortunately it seems __PRETTY_FUNCTION__ contains
only the necesarry information if you are using C++,
and there you don't have nested functions.

The closest I could come up with was a switch on
sizeof. But if two of the types you might be using
are of the same size, then that isn't of much help.
Here is the code. Can somebody suggest a fix? Or is
this really impossible?

#include <stdio.h>
#define config_write(key, value) do { \
char *__foobar (typeof(value) x) { \
return __PRETTY_FUNCTION__; \
} \
do_config_write(key,__foobar(value),value);\
} while(0)
void do_config_write(long key, char *type, ...)
{
printf("%ld: %s\n",key,type);
}
int main()
{
int foobar=42;
config_write(27,foobar);
return 0;
}

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
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?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.