Error Ambiguous on signed / unsigned

N

Nephi Immortal

I have two types – signed long and unsigned long. Are they really
signed int type? If unsigned long type is chosen, signed int is
chosen by C++ Compiler unless I declare explicit cast or error
ambiguous is reported.

void Do( signed int x ) {}
void Do( unsigned int x ) {}

int main()
{
signed long a = -1;
unsigned long b = 1u;

Do( a ); // OK
Do( b ); // error ambiguous
Do( static_cast< unsigned int >( b )); // OK
};

How can I use implicit cast and let C++ Compiler match signed int or
unsigned int?
 
G

Geoff

I have two types – signed long and unsigned long. Are they really
signed int type? If unsigned long type is chosen, signed int is
chosen by C++ Compiler unless I declare explicit cast or error
ambiguous is reported.

void Do( signed int x ) {}
void Do( unsigned int x ) {}

int main()
{
signed long a = -1;
unsigned long b = 1u;

Do( a ); // OK
Do( b ); // error ambiguous
Do( static_cast< unsigned int >( b )); // OK
};

How can I use implicit cast and let C++ Compiler match signed int or
unsigned int?

You can't.

The ambiguity is not signed vs. unsigned but int vs long. The language
is protecting you from committing an error by stuffing a long argument
into an int without explicitly telling the compiler that you want it.

void Do( signed int x ) {}
void Do( unsigned int x ) {}
void Do( signed long x ) {}
void Do( unsigned long x ) {}

int main()
{
signed long a = -1;
unsigned long b = 1u;

Do( a ); // OK
Do( b ); // OK
Do( static_cast< unsigned int >( b )); // OK
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top