Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsigned long vs size_t causes function overload fail

I defined a function:

void myfunc(size_t param1, size_t param2){
...
}

it works fine. But when I try to overload this function

void myfunc(unsigned long param1, unsigned long param2){
...
}

It fails to compile with the following message: error: myfunc(unsigned long param1, unsigned long param2) cannot be overloaded.

How can I solve this problem without staic_cast the input parameters to size_t?

thanks!

like image 674
Harvey Dent Avatar asked Oct 20 '22 12:10

Harvey Dent


1 Answers

It sounds like size_t and unsigned long are the same type on your system; the compiler is complaining that you have two of the same function. Furthermore, overloading with multiple number types is generally a bad idea because the compiler may not be able to recognize which overload you want due to casting possibilities. Try using templates instead:

template <T>
void myfunc(T param1, T param2){
...
}
like image 142
sabreitweiser Avatar answered Oct 22 '22 21:10

sabreitweiser