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?
 
Ad

Advertisements

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

Top