Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function overloading and base types

Reply
Thread Tools

Function overloading and base types

 
 
Gordon Schumacher
Guest
Posts: n/a
 
      08-25-2006
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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-25-2006
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


 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      08-25-2006

"Victor Bazarov" <(E-Mail Removed)> skrev i meddelandet
news:ecltkf$jlb$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      08-25-2006
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<T> Lim;

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Containg Types Or Base Types rn5a@rediffmail.com ASP .Net 0 11-22-2006 10:18 PM
SOLVED: Function overloading and base types Gordon Schumacher C++ 0 08-26-2006 05:25 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Steve Holden Python 0 09-30-2005 01:58 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments