sailfish009
sailfish009

Reputation: 2929

error: call of overloaded function ambiguous

I am trying to compile ustl C++ library with MinGW GCC compiler on Windows 10.

The 41st line in the C++ header below was able to solve thanks to this answer, but there is a problem at line 43. How should I solve it?

https://github.com/msharov/ustl/blob/master/fstream.h#L41

    // int          ioctl (const char* rname, int request, long argument = 0);   // orig
    int         ioctl (const char* rname, int request, long argument = 0u);
    inline int      ioctl (const char* rname, int request, int argument)    { return fstream::ioctl (rname, request, long(argument)); }
    inline int      ioctl (const char* rname, int request, void* argument)  { return fstream::ioctl (rname, request, intptr_t(argument)); }

MinGW GCC compiler (10.3.0 (Rev5, Built by MSYS2 project)) error message:

fstream.h:43:132: error: call of overloaded 'ioctl(const char*&, int&, uintptr_t)' is ambiguous
    43 |     inline int  ioctl (const char* rname, int request, void* argument) { return fstream::ioctl (rname, request, uintptr_t(argument)); }

-- Edit: Thanks for the hint. With another answer, below code works for me:

https://github.com/msharov/ustl/blob/master/fstream.h#L43

 // ... intptr_t(argument));  // orig
 *((long*)(argument))); 

Upvotes: 0

Views: 1342

Answers (1)

SHP
SHP

Reputation: 315

You can find in the last few words that

fstream.h:43:132: error: call of overloaded 'ioctl(const char*&, int&, uintptr_t)' is ambiguous 43 | inline int ioctl (const char* rname, int request, void* argument) { return fstream::ioctl (rname, request, uintptr_t(argument)); }

intptr_t is actually a uintptr_t in your environment. And as for uintptr_t, you can find in cppreference that it's an unsigned integer.

According to ranking of implicit conversion sequence in overload resolution, unsigned sth => long and unsigned sth => int are equivalent(both conversion). So no winner, all loser, which leads to compilation error.

If you just want to avoid compilation error, convert intptr_t to long helps. But I think the best way is to use proper type that stands for its meaning. For example, if it's for an address, then just use uintptr_t in the beginning, and make it a convention.

Upvotes: 3

Related Questions