Unsigned long vs size_t causes overload function overload

I defined a function:

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

It works great. But when I try to overload this function

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

Cannot 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 input parameters in size_t?

thanks!

+5
source share
1 answer

It appears that size_t and unsigned long are of the same type on your system; the compiler complains that you have two identical functions. In addition, overloading with multiple types of numbers is generally a bad idea, because the compiler may not recognize which overload you want due to casting capabilities. Try templates instead:

 template <T> void myfunc(T param1, T param2){ ... } 
+3
source

All Articles