ambiguous call to div() - cstdlib vs stdlib.h

486 views
Skip to first unread message

gadget

unread,
Mar 20, 2013, 9:28:58 PM3/20/13
to andro...@googlegroups.com
I'm getting a strange error when compiling some code (using ndk-r8b) that uses a call to div(size_t, size_t). Here it is:

error: call of overloaded 'div(size_t&, size_t&)' is ambiguous
whatever.cpp:126:55: note: candidates are:
/Developer/Android/android-ndk-r8b/platforms/android-9/arch-arm/usr/include/stdlib.h:150:16: note: div_t div(int, int)
/Developer/Android/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/cstdlib:142:3: note: ldiv_t std::div(long int, long int)

Changing the offending call to std::div does not help. The problem appears to be that both stdlib.h and cstdlib are being included by parts of the code, independently of each other. The project is compiled with APP_STL := gnustl_static.

I was wondering what would be the best way of dealing with this?

a1

unread,
Mar 21, 2013, 2:02:56 AM3/21/13
to andro...@googlegroups.com
W dniu czwartek, 21 marca 2013 02:28:58 UTC+1 użytkownik gadget napisał:
I'm getting a strange error when compiling some code (using ndk-r8b) that uses a call to div(size_t, size_t). Here it is:

error: call of overloaded 'div(size_t&, size_t&)' is ambiguous
whatever.cpp:126:55: note: candidates are:
/Developer/Android/android-ndk-r8b/platforms/android-9/arch-arm/usr/include/stdlib.h:150:16: note: div_t div(int, int)
/Developer/Android/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/cstdlib:142:3: note: ldiv_t std::div(long int, long int)


It's correct behaviour - both overloads may be used and both require cast between numerical types for argument - hence compiler desn't know which one to choose. You have to cast explicitly, that is: div(static_cast<long> (arg1), static_cast<long> (arg2));

--
Bart

Jeffrey Walton

unread,
Mar 21, 2013, 2:21:47 AM3/21/13
to andro...@googlegroups.com
size_t is unsigned, at least 16 bits, and can hold an object. I
believe the last is usually interpreted as a pointer, which is the
width of a machine word.

Perhaps uintptr_t would be a better choice?

Jeff

a1

unread,
Mar 21, 2013, 3:51:11 AM3/21/13
to andro...@googlegroups.com

size_t is unsigned, at least 16 bits, and can hold an object. I
believe the last is usually interpreted as a pointer, which is the
width of a machine word.

Perhaps uintptr_t would be a better choice?


I'm not quite sure what are you referring to, size_t is unsigned integer type and div is numerical operation, but there is no div() function overload for size_t hence both div() overload requires conversion (either to signed int or to signed long), and as a result overload cannot be selected automatically. Adding explicit cast to one of possible arguments type resolve this ambiguity. 

--
Bart

gadget

unread,
Mar 21, 2013, 1:24:25 PM3/21/13
to andro...@googlegroups.com
Bart, thank you for the clarification. Explicit casting to int's took care of it.
Reply all
Reply to author
Forward
0 new messages