473,511 Members | 13,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function overloading and base types

I'm working with a class with a set of functions that need to be able
to process any unsigned integer value, and I need this code to compile
on multiple platforms, so I have something similar to:

class A
{
uint8_t myFunc(uint8_t val);
uint16_t myFunc(uint16_t val);
uint32_t myFunc(uint32_t val);
uint64_t myFunc(uint64_t val);
};

This should, I would think, cover all my bases.
Elsewhere in the code, I have:

size_t foo, bar;
bar = myFunc(foo);

I am currently compiling on three platforms: MSVC++ 7.1, GCC 4.1.0 for
DOS (DJGPP), and GCC 4.0.2 for Linux. On the first two, this code
works fine; on Linux, though, I get an ambiguous overload error.

Looking at the preprocessor output, I found the difference: MSVC++ 7.1
and DJGPP both expand size_t to "unsigned int", and Linux expands it to
"long unsigned int". The uint32_t is typedef'ed to "unsigned int" on
all three platforms - thus matching size_t on all but Linux. If, as a
test, I change my function declarations to:

class A
{
unsigned char myFunc(unsigned char val);
unsigned short myFunc(unsigned short val);
unsigned int myFunc(unsigned int val);
unsigned long myFunc(unsigned long val);
unsigned long long myFunc(unsigned long long val);
};

that is to say, include definitions for all three of short, int, and
long... then all three platforms are happy.

As it happens, on all three of these platforms, "unsigned int" and
"unsigned long" are the same size. This leads to difficulties when the
implementations of those functions are dependent on the parameter's
size (in this case, I'm doing byte-swapping!)

I also can't safely just add an additional definition for a "size_t"
type to the first implementation, because then the *other* two
platforms have duplicate definitions of "unsigned int"!

There must be a solution for this, but I'll be darned if I can figure
out what it is. Help?

Aug 25 '06 #1
3 1804
Gordon Schumacher wrote:
I'm working with a class with a set of functions that need to be able
to process any unsigned integer value, and I need this code to compile
on multiple platforms, so I have something similar to:

class A
{
uint8_t myFunc(uint8_t val);
uint16_t myFunc(uint16_t val);
uint32_t myFunc(uint32_t val);
uint64_t myFunc(uint64_t val);
};

This should, I would think, cover all my bases.
Elsewhere in the code, I have:

size_t foo, bar;
bar = myFunc(foo);

I am currently compiling on three platforms: MSVC++ 7.1, GCC 4.1.0 for
DOS (DJGPP), and GCC 4.0.2 for Linux. On the first two, this code
works fine; on Linux, though, I get an ambiguous overload error.

Looking at the preprocessor output, I found the difference: MSVC++ 7.1
and DJGPP both expand size_t to "unsigned int", and Linux expands it
to "long unsigned int". The uint32_t is typedef'ed to "unsigned int"
on all three platforms - thus matching size_t on all but Linux. If,
as a test, I change my function declarations to:

class A
{
unsigned char myFunc(unsigned char val);
unsigned short myFunc(unsigned short val);
unsigned int myFunc(unsigned int val);
unsigned long myFunc(unsigned long val);
unsigned long long myFunc(unsigned long long val);
};

that is to say, include definitions for all three of short, int, and
long... then all three platforms are happy.

As it happens, on all three of these platforms, "unsigned int" and
"unsigned long" are the same size. This leads to difficulties when
the implementations of those functions are dependent on the
parameter's size (in this case, I'm doing byte-swapping!)

I also can't safely just add an additional definition for a "size_t"
type to the first implementation, because then the *other* two
platforms have duplicate definitions of "unsigned int"!

There must be a solution for this, but I'll be darned if I can figure
out what it is. Help?
OK, I'll let you in on a secret. When doing multi-platform development,
you cannot escape having to do some #ifdef'ing:

class A
{
unsigned char ...
#if defined ( LINUX ) // or whatever your compiler defines
unsigned somesuch myFunc( ...
#elif defined ( WIN32 ) || defined ( MSDOS )
unsigned otherthing myFunc( ...
#endif
..
};

That should make your compilers happier.

Now, if you want to do platform-independent programming (which you
probably don't), you need to consider that 'unsigned char', 'unsigned
short', 'unsigned int', and 'unsigned long' can all be the same size.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 25 '06 #2

"Victor Bazarov" <v.********@comAcast.netskrev i meddelandet
news:ec**********@news.datemas.de...
Gordon Schumacher wrote:
>I'm working with a class with a set of functions that need to be
able
to process any unsigned integer value, and I need this code to
compile
on multiple platforms, so I have something similar to:

class A
{
uint8_t myFunc(uint8_t val);
uint16_t myFunc(uint16_t val);
uint32_t myFunc(uint32_t val);
uint64_t myFunc(uint64_t val);
};

There must be a solution for this, but I'll be darned if I can
figure
out what it is. Help?

OK, I'll let you in on a secret. When doing multi-platform
development,
you cannot escape having to do some #ifdef'ing:

class A
{
unsigned char ...
#if defined ( LINUX ) // or whatever your compiler defines
unsigned somesuch myFunc( ...
#elif defined ( WIN32 ) || defined ( MSDOS )
unsigned otherthing myFunc( ...
#endif
..
};

That should make your compilers happier.
The other way is to have different implementations for each supported
platform, in separate directories. You can then set the proper include
path for each compiler.
>
Now, if you want to do platform-independent programming (which you
probably don't), you need to consider that 'unsigned char',
'unsigned
short', 'unsigned int', and 'unsigned long' can all be the same
size.
And when doing byte swapping, we have to consider all the byte orders.
There are way more than two!
Bo Persson
Aug 25 '06 #3
Gordon Schumacher posted:
I'm working with a class with a set of functions that need to be able
to process any unsigned integer value, and I need this code to compile
on multiple platforms, so I have something similar to:

Here's how I'd do it. First, I'd start with a method of performing a
compile-time assert:

template <int i>
struct VerifyType {
unsigned bit_field_cant_be_negative : i;
};

#define COMPASS(expr) \
int (*VerifyFuncDecl()) [ \
((void)sizeof(VerifyType<(expr) ? 1 : -1 >), 2) \
]

Then I'd use it as follows:

#include <limits>

template<class T>
T MyFunc(T const arg)
{
typedef std::numeric_limits<TLim;

COMPASS(Lim::is_integer);
COMPASS(!Lim::is_signed);

/* Now it's safe to proceed... */
}

For some reason, it won't compile with g++, I get a gibberish error.

--

Frederick Gotham
Aug 25 '06 #4

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. ...
45
3219
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
2
2498
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
6
4995
by: flopbucket | last post by:
Could someone explain to me what the difference is between function template specialization and function overloading? I guess overloading can change the number of parameters, but otherwise they...
0
1423
by: Gordon Schumacher | last post by:
I found a clever way to solve this issue: I used a variant of the code at http://www.flipcode.com/cgi-bin/fcarticles.cgi?show=64171. On one hand, this code is fairly arcane, and I realize that...
7
6102
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
11
28108
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
14
562
by: Pramod | last post by:
I have one question. Can I catch exception in function overloading.In my programm i want to create two function with same signature and same return type. I know its not possible...can i use...
3
2328
by: Markus Dehmann | last post by:
I have an abstract base class which contains a function that I would like to template, but virtual template functions are illegal. I put a mini code example below, which doesn't do anything great,...
0
7245
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
7144
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...
1
7085
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...
1
5069
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
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
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...
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.